久攻不克的搜索题
1.http://acm.hdu.edu.cn/showproblem.php?pid=1180
貌似就是只要分清楚各种情况就能AC,我自己觉得是该想的都想了,还是不能AC,只有先留着以后再做了。。
下面是这题的各种错误代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 struct Pos 7 { 8 int x, y; 9 int step; 10 }S,E; 11 char map[25][25]; 12 bool used[25][25]; 13 int dir[][2]={1,0, -1,0, 0,1, 0,-1}; 14 int n, m; 15 16 bool Judge (Pos a) 17 { 18 if (a.x<0||a.x>n-1 || a.y<0||a.y>m-1 || used[a.x][a.y] || map[a.x][a.y]=='*') 19 return false; 20 return true; 21 } 22 23 int bfs () 24 { 25 Pos Pre, Cur; 26 queue <Pos> Q; 27 while (!Q.empty ()) Q.pop(); 28 memset (used, 0, sizeof used); 29 30 used[S.x][S.y] = true; 31 Q.push (S); 32 while (!Q.empty ()) 33 { 34 Pre = Q.front (); 35 Q.pop(); 36 for (int i=0; i<4; i++) 37 { 38 Cur = Pre; 39 Cur.x += dir[i][0]; 40 Cur.y += dir[i][1]; 41 Cur.step += 1; 42 if (Judge (Cur)) 43 { 44 if (map[Cur.x][Cur.y]=='|') 45 { 46 Pos tmp = Cur; 47 if ((i==2 || i==3) && Cur.step%2==0) 48 { 49 tmp.x = Cur.x + dir[i][0]; 50 tmp.y = Cur.y + dir[i][1]; 51 if (Judge (tmp)) 52 { 53 used[tmp.x][tmp.y] = true; 54 Q.push (tmp); 55 } 56 } 57 else if ((i==0 || i==1) && Cur.step%2) 58 { 59 tmp.x = Cur.x + dir[i][0]; 60 tmp.y = Cur.y + dir[i][1]; 61 if (Judge (tmp)) 62 { 63 used[tmp.x][tmp.y] = true; 64 Q.push (tmp); 65 } 66 } 67 } 68 else if (map[Cur.y][Cur.y]=='-') 69 { 70 Pos tmp = Cur; 71 if ((i==0 || i==1) && Cur.step%2==0) 72 { 73 tmp.x = Cur.x + dir[i][0]; 74 tmp.y = Cur.y + dir[i][1]; 75 if (Judge (tmp)) 76 { 77 used[tmp.x][tmp.y] = true; 78 Q.push (tmp); 79 } 80 } 81 else if ((i==2 || i==3) && Cur.step%2) 82 { 83 tmp.x = Cur.x + dir[i][0]; 84 tmp.y = Cur.y + dir[i][1]; 85 if (Judge (tmp)) 86 { 87 used[tmp.x][tmp.y] = true; 88 Q.push (tmp); 89 } 90 } 91 } 92 else if (map[Cur.x][Cur.y]=='T') 93 return Cur.step; 94 used[Cur.x][Cur.y] = true; 95 Q.push (Cur); 96 } 97 } 98 } 99 return -1; 100 } 101 int main() 102 { 103 // freopen ("test.txt","r",stdin); 104 while (~scanf ("%d%d",&n, &m)) 105 { 106 for (int i=0; i<n; i++) 107 for (int j=0; j<m;j++) 108 { 109 scanf (" %c",map[i]+j); 110 if (map[i][j] == 'S') 111 S.x =i,S.y=j, S.step=0; 112 else if (map[i][j]=='T') 113 E.x=i, E.y=j; 114 } 115 116 int ans = bfs(); 117 printf ("%d\n",ans); 118 } 119 return 0; 120 }
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 struct Pos 7 { 8 int x, y; 9 int step; 10 }S,E; 11 char map[25][25]; 12 bool used[25][25]; 13 int dir[][2]={1,0, -1,0, 0,1, 0,-1}; 14 int n, m; 15 16 bool Judge (Pos a) 17 { 18 if (a.x<0||a.x>n-1 || a.y<0||a.y>m-1 || used[a.x][a.y] || map[a.x][a.y]=='*') 19 return false; 20 return true; 21 } 22 23 int bfs () 24 { 25 Pos Pre, Cur; 26 queue <Pos> Q; 27 while (!Q.empty ()) Q.pop(); 28 memset (used, 0, sizeof used); 29 30 used[S.x][S.y] = true; 31 Q.push (S); 32 while (!Q.empty ()) 33 { 34 Pre = Q.front (); 35 Q.pop(); 36 for (int i=0; i<4; i++) 37 { 38 Cur = Pre; 39 Cur.x += dir[i][0]; 40 Cur.y += dir[i][1]; 41 Cur.step += 1; 42 if (Judge (Cur)) 43 { 44 if (map[Cur.x][Cur.y]=='|') 45 { 46 Pos tmp = Cur; 47 if ((i==2 || i==3) && Cur.step%2==0) 48 { 49 tmp.x = Cur.x + dir[i][0]; 50 tmp.y = Cur.y + dir[i][1]; 51 if (Judge (tmp)) 52 { 53 used[tmp.x][tmp.y] = true; 54 Q.push (tmp); 55 } 56 } 57 else if ((i==0 || i==1) && Cur.step%2) 58 { 59 tmp.x = Cur.x + dir[i][0]; 60 tmp.y = Cur.y + dir[i][1]; 61 if (Judge (tmp)) 62 { 63 used[tmp.x][tmp.y] = true; 64 Q.push (tmp); 65 } 66 } 67 else 68 { 69 Cur.step++; 70 Q.push(Cur); 71 continue; 72 } 73 } 74 else if (map[Cur.y][Cur.y]=='-') 75 { 76 Pos tmp = Cur; 77 if ((i==0 || i==1) && Cur.step%2==0) 78 { 79 tmp.x = Cur.x + dir[i][0]; 80 tmp.y = Cur.y + dir[i][1]; 81 if (Judge (tmp)) 82 { 83 used[tmp.x][tmp.y] = true; 84 Q.push (tmp); 85 } 86 } 87 else if ((i==2 || i==3) && Cur.step%2) 88 { 89 tmp.x = Cur.x + dir[i][0]; 90 tmp.y = Cur.y + dir[i][1]; 91 if (Judge (tmp)) 92 { 93 used[tmp.x][tmp.y] = true; 94 Q.push (tmp); 95 } 96 } 97 else 98 { 99 Cur.step++; 100 Q.push (Cur); 101 continue; 102 } 103 } 104 else if (map[Cur.x][Cur.y]=='T') 105 return Cur.step; 106 used[Cur.x][Cur.y] = true; 107 Q.push (Cur); 108 } 109 } 110 } 111 return -1; 112 } 113 int main() 114 { 115 // freopen ("test.txt","r",stdin); 116 while (~scanf ("%d%d",&n, &m)) 117 { 118 for (int i=0; i<n; i++) 119 for (int j=0; j<m;j++) 120 { 121 scanf (" %c",map[i]+j); 122 if (map[i][j] == 'S') 123 S.x =i,S.y=j, S.step=0; 124 else if (map[i][j]=='T') 125 E.x=i, E.y=j; 126 } 127 128 int ans = bfs(); 129 printf ("%d\n",ans); 130 } 131 return 0; 132 }
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #define Min(a, b) (a < b ? a : b) 5 using namespace std; 6 7 struct Pos 8 { 9 int x, y; 10 int step; 11 }S,E; 12 char map[25][25]; 13 bool used[25][25]; 14 int dir[][2]={1,0, -1,0, 0,1, 0,-1}; 15 int n, m; 16 17 bool Judge (Pos a) 18 { 19 if (a.x<0||a.x>n-1 || a.y<0||a.y>m-1 || used[a.x][a.y] || map[a.x][a.y]=='*') 20 return false; 21 return true; 22 } 23 24 int bfs () 25 { 26 int ans = 0x3f3f3f3f; 27 Pos Pre, Cur; 28 queue <Pos> Q; 29 while (!Q.empty ()) Q.pop(); 30 memset (used, 0, sizeof used); 31 32 used[S.x][S.y] = true; 33 Q.push (S); 34 while (!Q.empty ()) 35 { 36 Pre = Q.front (); 37 Q.pop(); 38 for(int i=0; i<4; i++) 39 { 40 Cur = Pre; 41 Cur.x += dir[i][0]; 42 Cur.y += dir[i][1]; 43 Cur.step += 1; 44 if (map[Cur.x][Cur.y]=='T') 45 ans = Min(ans, Cur.step); 46 if (Judge (Cur)) 47 { 48 if (map[Cur.x][Cur.y]=='|') 49 { 50 if ((Pre.step%2==0 && (i==2 || i==3)) || (Pre.step%2==1 && (i==1 || i==0))) 51 { 52 Cur.x += dir[i][0]; 53 Cur.y += dir[i][1]; 54 if (Judge (Cur)) 55 { 56 if(map[Cur.x][Cur.y]=='T') 57 ans = Min (ans, Cur.step); 58 used[Cur.x][Cur.y] = true; 59 Q.push (Cur); 60 } 61 } 62 else 63 { 64 Cur.x += dir[i][0]; 65 Cur.y += dir[i][1]; 66 Cur.step += 1; 67 if (Judge (Cur)) 68 { 69 if (map[Cur.x][Cur.y]=='T') 70 ans = Min (ans, Cur.step); 71 used[Cur.x][Cur.y] = true; 72 Q.push (Cur); 73 } 74 } 75 continue; 76 } 77 else if (map[Cur.x][Cur.y]=='-') 78 { 79 if ((Pre.step%2==0 && (i==0 || i==1)) || (Pre.step%2==1 && (i==2 || i==3))) 80 { 81 Cur.x += dir[i][0]; 82 Cur.y += dir[i][1]; 83 if (Judge (Cur)) 84 { 85 if (map[Cur.x][Cur.y]=='T') 86 ans = Min (ans, Cur.step); 87 used[Cur.x][Cur.y] = true; 88 Q.push (Cur); 89 } 90 } 91 else 92 { 93 Cur.x += dir[i][0]; 94 Cur.y += dir[i][1]; 95 Cur.step += 1; 96 if (Judge (Cur)) 97 { 98 if (map[Cur.x][Cur.y]=='T') 99 ans = Min (ans, Cur.step); 100 used[Cur.x][Cur.y] = true; 101 Q.push (Cur); 102 } 103 } 104 continue; 105 } 106 else if (map[Cur.x][Cur.y] == 'T') 107 ans = Min (ans, Cur.step); 108 109 used[Cur.x][Cur.y] = true; 110 111 Q.push (Cur); 112 } 113 } 114 } 115 return ans; 116 } 117 int main() 118 { 119 // freopen ("test.txt","r",stdin); 120 while (~scanf ("%d%d",&n, &m)) 121 { 122 for (int i=0; i<n; i++) 123 for (int j=0; j<m;j++) 124 { 125 scanf (" %c",map[i]+j); 126 if (map[i][j] == 'S') 127 S.x =i,S.y=j, S.step=0; 128 } 129 130 int ans = bfs(); 131 132 printf ("%d\n",ans); 133 } 134 return 0; 135 }
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 #define N 250 6 7 struct Pos 8 { 9 int x, y; 10 int step; 11 }S; 12 char map[N][N]; 13 bool used[N][N]; 14 int n, m, dir[][2]={1,0, -1,0, 0,1, 0,-1}; 15 16 bool Judge (Pos a) 17 { 18 if (a.x<0||a.x>n-1 || a.y<0||a.y>m-1 || used[a.x][a.y] || map[a.x][a.y]=='*') 19 return false; 20 return true; 21 } 22 23 int bfs () 24 { 25 Pos Pre, Cur; 26 queue <Pos> Q; 27 memset (used, 0, sizeof used); 28 29 used[S.x][S.y] = true; 30 Q.push (S); 31 while (!Q.empty()) 32 { 33 Pre = Q.front(); 34 Q.pop(); 35 if (map[Pre.x][Pre.y] == 'T') 36 return Pre.step; 37 for (int i=0; i<4; i++) 38 { 39 Cur = Pre; 40 Cur.x += dir[i][0]; 41 Cur.y += dir[i][1]; 42 Cur.step += 1; 43 if (Judge (Cur)) 44 { 45 Pos tmp = Cur; 46 if (map[Cur.x][Cur.y]=='|') 47 { 48 if ((Pre.step%2==1 && (i==0 || i==1)) || (Pre.step%2==0 && (i==2 || i==3))) 49 { 50 tmp.x += dir[i][0]; 51 tmp.y += dir[i][1]; 52 Q.push (tmp); 53 } 54 else 55 { 56 tmp.x += dir[i][0]; 57 tmp.y += dir[i][1]; 58 tmp.step += 1; 59 Q.push (tmp); 60 } 61 used[tmp.x][tmp.y] = true; 62 } 63 else if (map[Cur.x][Cur.y]=='-') 64 { 65 if ((Pre.step%2==1 && (i==2 || i==3)) || (Pre.step%2==0 && (i==0 || i==1))) 66 { 67 tmp.x += dir[i][0]; 68 tmp.y += dir[i][1]; 69 Q.push (tmp); 70 } 71 else 72 { 73 tmp.x += dir[i][0]; 74 tmp.y += dir[i][1]; 75 tmp.step += 1; 76 Q.push (tmp); 77 } 78 used[tmp.x][tmp.y] = true; 79 } 80 used[Cur.x][Cur.y] = true; 81 Q.push (Cur); 82 } 83 } 84 } 85 return -1; 86 } 87 88 int main() 89 { 90 while (~scanf ("%d%d",&n, &m)) 91 { 92 for (int i=0; i<n; i++) 93 for (int j=0; j<m; j++) 94 { 95 scanf (" %c",map[i]+j); 96 if (map[i][j]=='S') 97 S.x=i, S.y=j, S.step=0; 98 } 99 int ans = bfs (); 100 printf ("%d\n",ans); 101 } 102 return 0; 103 }
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 using namespace std; 5 6 struct Pos 7 { 8 int x, y; 9 int step; 10 }S; 11 int n, m; 12 char map[21][21]; 13 bool used[21][21]; 14 int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}}; 15 16 void CreatMap () 17 { 18 for (int i=0; i<m; i++) 19 for (int j=0; j<n; j++) 20 { 21 scanf (" %c",&map[i][j]); 22 if (map[i][j]=='S') 23 { 24 S.x = i; S.y = j; 25 } 26 } 27 } 28 29 bool Judge (Pos a) 30 { 31 if (a.x<0||a.x>n-1 || a.y<0||a.y>m-1 || used[a.x][a.y] || map[a.x][a.y]=='*') 32 return false; 33 return true; 34 } 35 36 int BFS () 37 { 38 queue <Pos> Q; 39 Pos Pre, Cur; 40 memset (used, 0, sizeof (used)); 41 42 S.step = 0; 43 used[S.x][S.y] = true; 44 Q.push (S); 45 while (!Q.empty ()) 46 { 47 Pre = Q.front (); 48 Q.pop (); 49 if (map[Pre.x][Pre.y]=='T') 50 return Pre.step; 51 52 for (int i=0; i<4; i++) 53 { 54 Cur = Pre; 55 Cur.x += dir[i][0]; 56 Cur.y += dir[i][1]; 57 Cur.step += 1; 58 59 if (Judge (Cur)) 60 { 61 if (map[Cur.x][Cur.y]=='-' || map[Cur.x][Cur.y]=='|') 62 { 63 Pos tmp = Cur; 64 tmp.x += dir[i][0]; 65 tmp.y += dir[i][1]; 66 if (Judge (tmp)) 67 { 68 if ((map[Cur.x][Cur.y]=='|' && Pre.step%2==1 && i>1) || 69 (map[Cur.x][Cur.y]=='|' && Pre.step%2==0 && i<2) || 70 (map[Cur.x][Cur.y]=='-' && Pre.step%2==1 && i<2) || 71 (map[Cur.x][Cur.y]=='-' && Pre.step%2==0 && i>1)) 72 tmp.step++; 73 74 used[tmp.x][tmp.y] = true; 75 Q.push (tmp); 76 } 77 } 78 else 79 { 80 used[Cur.x][Cur.y] = true; 81 Q.push (Cur); 82 } 83 } 84 } 85 } 86 return -1; 87 } 88 89 int main () 90 { 91 while (~scanf ("%d%d",&m,&n)) 92 { 93 CreatMap (); 94 95 printf ("%d\n",BFS ()); 96 } 97 return 0; 98 }
2.http://acm.hdu.edu.cn/showproblem.php?pid=1045
题意:在一个正方形中,‘X'表示墙,’.'表示空地。可以再空地上放上大炮。因为在同一行或同一列的大炮且没有强隔开的直线上,大炮可以相互攻击,问你在大炮不能相互攻击的情况下可以摆放的最多的大炮数是多少???