View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 int board[110][110]; 6 int di,dj,xi,xj,n,m,num,dis[4][2] = {0,1,0,-1,1,0,-1,0}; 7 void dfs(int v,int x,int y) 8 { 9 int i,tx,ty,flag = 0; 10 if(v>10) 11 return ; 12 for(i = 0 ; i < 4 ; i++) 13 { 14 tx = x; 15 ty = y; 16 flag = 0; 17 if(board[x+dis[i][0]][y+dis[i][1]]==1) 18 continue; 19 while(1) 20 { 21 tx = tx+dis[i][0]; 22 ty = ty+dis[i][1]; 23 if(tx<0||tx>n||ty<0||ty>m) 24 break; 25 if(board[tx][ty]==3) 26 { 27 if(num>v) 28 num = v; 29 break; 30 } 31 if(board[tx][ty]==1) 32 { 33 flag = 1; 34 break; 35 } 36 } 37 if(flag) 38 { 39 board[tx][ty] = 0; 40 dfs(v+1,tx-dis[i][0],ty-dis[i][1]); 41 board[tx][ty] = 1; 42 } 43 } 44 } 45 int main() 46 { 47 int i,j; 48 while(cin>>m>>n) 49 { 50 if(m==0&&n==0) 51 break; 52 num = 15; 53 for(i = 1; i <= n ; i++) 54 for(j = 1; j <= m ; j++) 55 { 56 cin>>board[i][j]; 57 if(board[i][j]==2) 58 { 59 xi = i; 60 xj = j; 61 } 62 if(board[i][j]==3) 63 { 64 di = i; 65 dj = j; 66 } 67 } 68 dfs(1,xi,xj); 69 if(num>10) 70 cout<<"-1\n"; 71 else 72 cout<<num<<endl; 73 } 74 return 0; 75 }
http://poj.org/problem?id=3009
求最短 直接就用了BFS 结果ME 后有看了人家写的dfs 写了一份dfs 更新最小值
View Code
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char s[50][50],so[2]; 6 int pr[5],dis[4][2]={-1,0,0,-1,1,0,0,1},vis[50][50],n,m,flag; 7 int dir[2][4][4] = {{{1, 0, 3, 2}, {2, 1, 0, 3}, {3, 2, 1, 0}, {0, 3, 2, 1}},{{3, 0, 1, 2}, {0, 1, 2, 3}, {1, 2, 3, 0}, {2, 3, 0, 1}}}; 8 struct node 9 { 10 int x,y,num; 11 }q[100010]; 12 int judge(int x,int y) 13 { 14 if(x<1||x>n||y<1||y>m) 15 return 0; 16 if(s[x][y]=='#') 17 return 0; 18 return 1; 19 } 20 void bfs() 21 { 22 int i,j,p = 0, d = 1; 23 memset(vis,0,sizeof(vis)); 24 q[d].x = so[0]; 25 q[d].y = so[1]; 26 q[d].num = 1; 27 while(p!=d) 28 { 29 p++; 30 int tx = q[p].x; 31 int ty = q[p].y; 32 int tnum = q[p].num; 33 if(s[tx][ty]=='E') 34 { 35 pr[2] = tnum; 36 break; 37 } 38 for(i = 0 ; i < 4 ; i++) 39 { 40 int nx = tx+dis[i][0]; 41 int ny = ty+dis[i][1]; 42 if(!vis[nx][ny]&&judge(nx,ny)) 43 { 44 vis[nx][ny] = 1; 45 d++; 46 q[d].x = nx; 47 q[d].y = ny; 48 q[d].num = tnum+1; 49 } 50 } 51 } 52 } 53 void dfs(int x,int y,int d,int f) 54 { 55 pr[f]++; 56 if(s[x][y]=='E') 57 return ; 58 int i,j; 59 if(f==1) 60 cout<<x<<" "<<y<<endl; 61 for(j = 0 ; j < 4 ; j++) 62 { 63 int td = dir[f][d][j]; 64 int tx = x+dis[td][0]; 65 int ty = y+dis[td][1]; 66 if(!vis[tx][ty]&&judge(tx,ty)) 67 { 68 vis[tx][ty] = 1; 69 dfs(tx,ty,td,f); 70 } 71 } 72 } 73 int main() 74 { 75 int i,j,k,t,in; 76 cin>>t; 77 while(t--) 78 { 79 cin>>m>>n; 80 pr[0] = 1; 81 pr[1] = 1; 82 for(i = 1 ; i <= n ; i++) 83 { 84 getchar(); 85 for(j = 1 ; j <= m ; j++) 86 { 87 cin>>s[i][j]; 88 if(s[i][j]=='S') 89 { 90 so[0] = i; 91 so[1] = j; 92 if(so[0]==n) 93 in = 0; 94 if(so[0]==1) 95 in = 2; 96 if(so[1]==m) 97 in = 1; 98 if(so[1]==1) 99 in = 3; 100 } 101 } 102 } 103 bfs(); 104 for(i = 0 ; i < 2 ; i++) 105 { 106 memset(vis,0,sizeof(vis)); 107 vis[so[0]+dis[in][0]][so[1]+dis[in][1]] = 1; 108 dfs(so[0]+dis[in][0],so[1]+dis[in][1],in,i); 109 } 110 cout<<pr[0]<<" "<<pr[1]<<" "<<pr[2]<<endl; 111 } 112 return 0; 113 }