http://poj.org/problem?id=2243
擦了个擦的,N久前在杭电上做的这题0ms,到poj上一交直接超时!poj根本不让用stl啊。。
没办法,只能模拟队列又乱敲了遍。
code1:
#include<cstdio>
#include<queue>
using namespace std ;
char bstr[3], estr[3] ;
int b[2], e[2] ;
int tur[8][2] = {-1, -2, 1, -2, -2, -1, 2, -1, -1, 2, 1, 2, -2, 1, 2, 1} ;
struct Point{
int x ;
int y ;
int step ;
};
void bfs(){
Point begin ;
begin.x = b[0] ;
begin.y = b[1] ;
begin.step = 0 ;
queue<Point> q ;
q.push(begin) ;
while(!q.empty()){
Point p = q.front() ;
q.pop() ;
for(int k=0; k<8; k++){
Point temp = p ;
temp.x += tur[k][0] ;
temp.y += tur[k][1] ;
if(temp.x<0||temp.x>7||temp.y<0||temp.y>7)
continue ;
temp.step ++ ;
if(temp.x==e[0]&&temp.y==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, temp.step) ;
return ;
}
q.push(temp) ;
}
}
return ;
}
int main(){
while(~scanf("%s%s", bstr, estr)){
b[0] = bstr[0] - 'a' ;
b[1] = bstr[1] - '1' ;
e[0] = estr[0] - 'a' ;
e[1] = estr[1] - '1' ;
bstr[2] = '\0' ;//用%s输出字符数组必须封好
estr[2] = '\0' ;
if(b[0]==e[0]&&b[1]==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, 0) ;
continue ;
}
bfs() ;
}
return 0 ;
#include<queue>
using namespace std ;
char bstr[3], estr[3] ;
int b[2], e[2] ;
int tur[8][2] = {-1, -2, 1, -2, -2, -1, 2, -1, -1, 2, 1, 2, -2, 1, 2, 1} ;
struct Point{
int x ;
int y ;
int step ;
};
void bfs(){
Point begin ;
begin.x = b[0] ;
begin.y = b[1] ;
begin.step = 0 ;
queue<Point> q ;
q.push(begin) ;
while(!q.empty()){
Point p = q.front() ;
q.pop() ;
for(int k=0; k<8; k++){
Point temp = p ;
temp.x += tur[k][0] ;
temp.y += tur[k][1] ;
if(temp.x<0||temp.x>7||temp.y<0||temp.y>7)
continue ;
temp.step ++ ;
if(temp.x==e[0]&&temp.y==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, temp.step) ;
return ;
}
q.push(temp) ;
}
}
return ;
}
int main(){
while(~scanf("%s%s", bstr, estr)){
b[0] = bstr[0] - 'a' ;
b[1] = bstr[1] - '1' ;
e[0] = estr[0] - 'a' ;
e[1] = estr[1] - '1' ;
bstr[2] = '\0' ;//用%s输出字符数组必须封好
estr[2] = '\0' ;
if(b[0]==e[0]&&b[1]==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, 0) ;
continue ;
}
bfs() ;
}
return 0 ;
}
code2:
#include<cstdio>
using namespace std ;
char bstr[3], estr[3] ;
int b[2], e[2] ;
int tur[8][2] = {-1, -2, 1, -2, -2, -1, 2, -1, -1, 2, 1, 2, -2, 1, 2, 1} ;
struct Point{
int x ;
int y ;
int step ;
}q[10000] ;
void bfs(){
Point begin ;
int h, r ;
h = 0, r = 1 ;
begin.x = b[0] ;
begin.y = b[1] ;
begin.step = 0 ;
q[0] = begin ;
while(r>h){
Point p = q[h++] ;
for(int k=0; k<8; k++){
Point temp = p ;
temp.x += tur[k][0] ;
temp.y += tur[k][1] ;
if(temp.x<0||temp.x>7||temp.y<0||temp.y>7)
continue ;
temp.step ++ ;
if(temp.x==e[0]&&temp.y==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, temp.step) ;
return ;
}
q[r++] = temp ;
}
}
return ;
}
int main(){
while(~scanf("%s%s", bstr, estr)){
b[0] = bstr[0] - 'a' ;
b[1] = bstr[1] - '1' ;
e[0] = estr[0] - 'a' ;
e[1] = estr[1] - '1' ;
bstr[2] = '\0' ;//用%s输出字符数组必须封好
estr[2] = '\0' ;
if(b[0]==e[0]&&b[1]==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, 0) ;
continue ;
}
bfs() ;
}
return 0 ;
using namespace std ;
char bstr[3], estr[3] ;
int b[2], e[2] ;
int tur[8][2] = {-1, -2, 1, -2, -2, -1, 2, -1, -1, 2, 1, 2, -2, 1, 2, 1} ;
struct Point{
int x ;
int y ;
int step ;
}q[10000] ;
void bfs(){
Point begin ;
int h, r ;
h = 0, r = 1 ;
begin.x = b[0] ;
begin.y = b[1] ;
begin.step = 0 ;
q[0] = begin ;
while(r>h){
Point p = q[h++] ;
for(int k=0; k<8; k++){
Point temp = p ;
temp.x += tur[k][0] ;
temp.y += tur[k][1] ;
if(temp.x<0||temp.x>7||temp.y<0||temp.y>7)
continue ;
temp.step ++ ;
if(temp.x==e[0]&&temp.y==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, temp.step) ;
return ;
}
q[r++] = temp ;
}
}
return ;
}
int main(){
while(~scanf("%s%s", bstr, estr)){
b[0] = bstr[0] - 'a' ;
b[1] = bstr[1] - '1' ;
e[0] = estr[0] - 'a' ;
e[1] = estr[1] - '1' ;
bstr[2] = '\0' ;//用%s输出字符数组必须封好
estr[2] = '\0' ;
if(b[0]==e[0]&&b[1]==e[1]){
printf("To get from %s to %s takes %d knight moves.\n", bstr, estr, 0) ;
continue ;
}
bfs() ;
}
return 0 ;
}