弱菜做了好久23333333........

传送门: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105278#overview

A 只能摆k个棋子,只能摆在#

  1. #include <cstring>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. const int max_n = ;
  6. char maze[max_n][max_n];
  7. bool vis[max_n];
  8. int n, m, ans, cnt;
  9. void init(){
  10. memset(maze, , sizeof(maze));
  11. ans = cnt = ;
  12. }
  13. void dfs(int k){
  14. if(cnt == m){ ++ans; return ; }
  15. if(k > n) return ;
  16. for(int i = ; i < n; ++i){
  17. if(maze[i][k] == '#' && !vis[i]){
  18. vis[i] = ; ++cnt;
  19. dfs(k+);
  20. vis[i] = ; --cnt;
  21. }
  22. }
  23. dfs(k+);
  24. }
  25. int main(){
  26. ios::sync_with_stdio(false);
  27. while(cin >> n >> m){
  28. if(n == m && n == -) break;
  29. init();
  30. for(int i = ; i < n; ++i){
  31. cin.get();
  32. for(int j = ; j < n; ++j) cin >> maze[i][j];
  33. }
  34. dfs();
  35. cout << ans << endl;
  36. }
  37. return ;
  38. }

B 三维BFS

  1. #include <cstdio>
  2. #include <queue>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. const int max_n = ;
  8. const int cx[] = {, , , -, , };
  9. const int cy[] = {, , -, , , };
  10. const int cz[] = {, , , , , -};
  11. int maze[max_n][max_n][max_n];
  12. bool vis[max_n][max_n][max_n];
  13. struct point{
  14. int x, y, z, step;
  15. };
  16. int L, R, C;
  17. int ex, ey, ez;
  18. int main(){
  19. ios::sync_with_stdio(false);
  20. while(cin >> L >> R >> C){
  21. if(L == && R == && C == ) break;
  22. queue<point> q;
  23. memset(vis, , sizeof(vis));
  24. memset(maze, , sizeof(maze));
  25. for(int k = ; k < L; ++k){
  26. for(int i = ; i < R; ++i){
  27. cin.get();
  28. for(int j = ; j < C; ++j){
  29. char ch = cin.get();
  30. if(ch == 'S'){
  31. maze[k][i][j] = ;
  32. point p = {k, i, j, };
  33. q.push(p);
  34. }
  35. else if(ch == '.') maze[k][i][j] = ;
  36. else if(ch == 'E'){
  37. maze[k][i][j] = ;
  38. ex = k; ey = i; ez = j;
  39. }
  40. }
  41. }
  42. cin.get();
  43. }
  44. int x, y, z, step;
  45. while(!q.empty()){
  46. point p = q.front();
  47. q.pop();
  48. x = p.x, y = p.y, z = p.z, step = p.step;
  49. if(x == ex && y == ey && z == ez) break;
  50. for(int i = ; i < ; ++i){
  51. int nx = x + cx[i], ny = y + cy[i], nz = z + cz[i];
  52. if(nx<||L<nx||ny<||R<ny||nz<||C<nz) continue;
  53. if(maze[nx][ny][nz]){
  54. maze[nx][ny][nz] = ;
  55. point tp = {nx, ny, nz, step + };
  56. q.push(tp);
  57. }
  58. }
  59. }
  60. if(x == ex && y == ey && z == ez) printf("Escaped in %d minute(s).\n", step);
  61. else printf("Trapped!\n");
  62. }
  63. return ;
  64. }

C 搜......

  1. #include <cstdio>
  2. #include <queue>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. struct point{
  8. int x, step;
  9. };
  10. int S, E;
  11. int vis[];
  12. int main(){
  13. ios::sync_with_stdio(false);
  14. while(cin >> S >> E){
  15. memset(vis, , sizeof(vis));
  16. vis[S] = ;
  17. queue<point> q;
  18. q.push(point{S, });
  19. int x, step;
  20. while(!q.empty()){
  21. point p = q.front();
  22. x = p.x; step = p.step;
  23. q.pop();
  24. if(x == E) break;
  25. for(int i = ; i < ; ++i){
  26. int nx;
  27. if(i == ) nx = x + ;
  28. else if(i == ) nx = x - ;
  29. else nx = x << ;
  30. if( <= nx && nx <= && !vis[nx]){ q.push(point{nx, step+}); vis[nx] = ; }
  31. }
  32. }
  33. cout << step << endl;
  34. }
  35. return ;
  36. }

D 卡了好久看了题解  第一行确定下来后其他行的也都确定下来了(说的好有道理),所以枚举第一行状态,确定下其他行,看最后一行是否都是0

  然后在这道题发现了之前二进制枚举的一个错误....... 正确姿势:(i>>j) & 1(吧?)

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int MAXN = ;
  9. const int INF = 0x3f3f3f3f;
  10. const int cx[] = {, , -, };
  11. const int cy[] = {, -, , };
  12. int n, m;
  13. int ans[MAXN][MAXN];
  14. int flip[MAXN][MAXN];
  15. int origin[MAXN][MAXN];
  16.  
  17. int check(int x, int y){
  18. int cnt = origin[x][y];
  19. for(int i = ; i < ; ++i){
  20. int nx = x + cx[i], ny = y + cy[i];
  21. if(nx >= && nx < n && ny >= && ny < m)
  22. cnt += flip[nx][ny];
  23. }
  24. return cnt & ;
  25. }
  26.  
  27. int duang(){
  28. for(int i = ; i < n; ++i)
  29. for(int j = ; j < m; ++j)
  30. if(check(i-, j)) flip[i][j] = ;
  31. for(int j = ; j < m; ++j)
  32. if(check(n-, j)) return ;
  33. int cnt = ;
  34. for(int i = ; i < n; ++i)
  35. for(int j = ; j < m; ++j)
  36. cnt += flip[i][j];
  37. return cnt;
  38. }
  39.  
  40. int main(){
  41. while(cin >> n >> m){
  42. memset(origin, , sizeof(origin));
  43. for(int i = ; i < n; ++i)
  44. for(int j = ; j < m; ++j)
  45. cin >> origin[i][j];
  46. int tans = INF;
  47. for(int i = ; i < (<<m); ++i){
  48. memset(flip, , sizeof(flip));
  49. for(int j = ; j < m; ++j)
  50. flip[][j] = (i>>j) & ;
  51. int cnt = duang();
  52. if(cnt < tans && cnt != ){
  53. tans = cnt;
  54. memcpy(ans, flip, sizeof(flip));
  55. }
  56. }
  57. if(tans == INF) cout << "IMPOSSIBLE" << endl;
  58. else{
  59. for(int i = ; i < n; ++i)
  60. for(int j = ; j < m; ++j)
  61. cout << ans[i][j] << " \n"[j==m-];
  62. }
  63. }
  64. return ;
  65. }

E 听说答案都在long long范围内hhhh.......

  1. #include <queue>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. typedef long long ll;
  6. int main(){
  7. ios::sync_with_stdio(false);
  8. int n;
  9. while(cin >> n){
  10. if(n == ) break;
  11. queue<ll> q;
  12. q.push(1LL);
  13. while(!q.empty()){
  14. ll k = q.front();
  15. q.pop();
  16. if(k % n == ){
  17. cout << k << endl;
  18. break;
  19. }
  20. q.push(k*);
  21. q.push(k*+);
  22. }
  23. }
  24. return ;
  25. }

F GOJ有这道题........

  1. #include <map>
  2. #include <set>
  3. #include <queue>
  4. #include <deque>
  5. #include <cmath>
  6. #include <string>
  7. #include <vector>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <cstring>
  11. #include <utility>
  12. #include <iostream>
  13. #include <algorithm>
  14. using namespace std;
  15. int m, n;
  16. bool isPrime[];
  17. int vis[];
  18. void preprocess(){
  19. memset(isPrime, true, sizeof(isPrime));
  20. isPrime[] = isPrime[] = false;
  21. for(int i = ; i < ; ++i){
  22. if(isPrime[i]){
  23. for(int j = i; i*j < ; ++j){
  24. isPrime[i*j] = false;
  25. }
  26. }
  27. }
  28. }
  29. void bfs(){
  30. memset(vis, , sizeof(vis));
  31. vis[m] = ;
  32. queue<int> q;
  33. q.push(m);
  34. while(!q.empty()){
  35. int k = q.front();
  36. q.pop();
  37. int d[] = {k/, k%/, k%/, k%};
  38. int b[] = {, , , };
  39. for(int i = ; i < ; ++i){
  40. for(int j = ; j < ; ++j){
  41. int t = k - d[i] * b[i] + j * b[i];
  42. if(k == n) return ;
  43. if(t > && isPrime[t] && !vis[t]){
  44. vis[t] = vis[k] + ;
  45. q.push(t);
  46. }
  47. }
  48. }
  49. }
  50. return ;
  51. }
  52. int main(){
  53. ios::sync_with_stdio(false);
  54. preprocess();
  55. int t;
  56. cin >> t;
  57. while(t--){
  58. cin >> m >> n;
  59. bfs();
  60. if(vis[n]) cout << vis[n]- << endl;
  61. else cout << "Impossible" << endl;
  62. }
  63. return ;
  64. }

G 水   直接模拟   略坑是第一张都是最底的一张

  1. #include <set>
  2. #include <string>
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. int main(){
  8. int t;
  9. cin >> t;
  10. for(int x = ; x <= t; ++x){
  11. int l;
  12. cin >> l;
  13. string str1, str2, desstr;
  14. cin >> str1 >> str2 >> desstr;
  15. int step = ;
  16. set<string> s;
  17. s.insert(str1+str2);
  18. bool flag = true;
  19. while(flag){
  20. string tmpstr;
  21. for(int i = ; i < l; ++i){
  22. tmpstr += str2[i];
  23. tmpstr += str1[i];
  24. }
  25. //cout << tmpstr << endl;
  26. ++step;
  27. if(tmpstr == desstr) break;
  28. if(s.count(tmpstr)){
  29. flag = false;
  30. break;
  31. }
  32. s.insert(tmpstr);
  33. str1 = tmpstr.substr(, l);
  34. str2 = tmpstr.substr(l, *l);
  35. //cout << str1 << " " << str2 << endl;
  36. }
  37. cout << x << ' ' << (flag ? step : -) << endl;
  38. }
  39. return ;
  40. }

H M题写吐了H不想做.......

还是搜    6种状态

I 暴力找两个#搜.......

  1. #include <queue>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int INF = 0x3f3f3f;
  9. const int cx[] = {, , , -};
  10. const int cy[] = {, -, , };
  11. int n, m;
  12. int dis[][];
  13. char map[][];
  14.  
  15. struct Point{
  16. int x, y;
  17. Point(){}
  18. Point(int xx, int yy) :x(xx), y(yy){}
  19. };
  20. queue<Point> q;
  21. int bfs(int x1, int y1, int x2, int y2){
  22. memset(dis, INF, sizeof(dis));
  23. q.push(Point(x1, y1));
  24. q.push(Point(x2, y2));
  25. dis[x1][y1] = ;
  26. dis[x2][y2] = ;
  27. while (!q.empty()){
  28. Point p = q.front();
  29. q.pop();
  30. for (int i = ; i < ; i++){
  31. int xx = p.x + cx[i], yy = p.y + cy[i];
  32. if (xx >= && xx < n && yy >= && yy<m && map[xx][yy] == '#' && dis[xx][yy] > dis[p.x][p.y] + ){
  33. dis[xx][yy] = dis[p.x][p.y] + ;
  34. q.push(Point(xx, yy));
  35. }
  36. }
  37. }
  38.  
  39. int ret = ;
  40. for (int i = ; i < n; i++)
  41. for (int j = ; j < m; j++)
  42. if (map[i][j] == '#')
  43. ret = max(ret, dis[i][j]);
  44. return ret;
  45. }
  46.  
  47. int main(){
  48. int t;
  49. cin >> t;
  50. for (int x = ; x <= t; x++){
  51. while(!q.empty()) q.pop();
  52. cin >> n >> m;
  53. for (int i = ; i < n; i++)
  54. cin >> map[i];
  55. int ans = INF;
  56. for (int i = ; i < n; i++)
  57. for (int j = ; j < m; j++)
  58. if (map[i][j] == '#')
  59. for (int ii = ; ii < n; ii++)
  60. for (int jj = ; jj < m; jj++)
  61. if (map[ii][jj] == '#'){
  62. int temp = bfs(i, j, ii, jj);
  63. ans = min(ans, temp);
  64. }
  65. if (ans == INF) ans = -;
  66. cout << "Case " << x << ": " << ans << endl;
  67. }
  68. return ;
  69. }

J 多处火.....先处理火,再处理人,遇到可行的情况退出来

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct pos{
  5. int x, y;
  6. pos(int xx = , int yy = ): x(xx), y(yy) {}
  7. };
  8. const int MAXN = ;
  9. const int INF = 0x7f7f7f7f;
  10. const int cx[] = {, , , -};
  11. const int cy[] = {, -, , };
  12. int n, m;
  13. int jx, jy, fx, fy;
  14. int maze[MAXN][MAXN];
  15. int jstep[MAXN][MAXN];
  16. int fstep[MAXN][MAXN];
  17.  
  18. int main(){
  19. int t;
  20. cin >> t;
  21. while(t--){
  22. cin >> n >> m;
  23. queue<pos> qf;
  24. memset(fstep, -, sizeof(fstep));
  25. for(int i = ; i < n; ++i){
  26. cin.get();
  27. for(int j = ; j < m; ++j){
  28. char ch;
  29. cin >> ch;
  30. if(ch == '#') maze[i][j] = ;
  31. else{
  32. maze[i][j] = ;
  33. if(ch == 'J'){
  34. jx = i;
  35. jy = j;
  36. }
  37. else if(ch == 'F'){
  38. fx = i;
  39. fy = j;
  40. qf.push(pos(fx, fy));
  41. fstep[fx][fy] = ;
  42. }
  43. }
  44. }
  45. }
  46. while(!qf.empty()){
  47. pos p = qf.front();
  48. int x = p.x, y = p.y;
  49. qf.pop();
  50. for(int i = ; i < ; ++i){
  51. int nx = x + cx[i], ny = y + cy[i];
  52. if(nx < || n <= nx || ny < || m <= ny || maze[nx][ny] == || fstep[nx][ny] != -) continue;
  53. qf.push(pos(nx, ny));
  54. fstep[nx][ny] = fstep[x][y] + ;
  55. }
  56. }
  57. // cout << endl;
  58. // for(int i = 0; i < n; ++i){
  59. // for(int j = 0; j < m; ++j)
  60. // cout << fstep[i][j] << '\t';
  61. // cout << endl;
  62. // }
  63. memset(jstep, -, sizeof(jstep));
  64. queue<pos> qj;
  65. qj.push(pos(jx, jy));
  66. jstep[jx][jy] = ;
  67. int ans = -;
  68. while(!qj.empty()){
  69. pos p = qj.front();
  70. int x = p.x, y = p.y;
  71. qj.pop();
  72. if(x == || x == n- || y == || y == m-){
  73. ans = jstep[x][y];
  74. break;
  75. }
  76. for(int i = ; i < ; ++i){
  77. int nx = x + cx[i], ny = y + cy[i];
  78. if(nx < || n <= nx || ny < || m <= ny || maze[nx][ny] == || jstep[nx][ny] != - || (fstep[nx][ny] != - && fstep[nx][ny] <= jstep[x][y] + )) continue;
  79. qj.push(pos(nx, ny));
  80. jstep[nx][ny] = jstep[x][y] + ;
  81. }
  82. }
  83. // cout << endl;
  84. // for(int i = 0; i < n; ++i){
  85. // for(int j = 0; j < m; ++j)
  86. // cout << jstep[i][j] << '\t';
  87. // cout << endl;
  88. // }
  89. if(ans == -) cout << "IMPOSSIBLE" << endl;
  90. else cout << ans+ << endl;
  91. }
  92. return ;
  93. }

K 搜   开数组记录上一个点的坐标

  1. #include <map>
  2. #include <set>
  3. #include <queue>
  4. #include <deque>
  5. #include <cmath>
  6. #include <stack>
  7. #include <string>
  8. #include <vector>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cstring>
  12. #include <utility>
  13. #include <iostream>
  14. #include <algorithm>
  15. using namespace std;
  16. struct point{
  17. int x, y;
  18. };
  19. int maze[][];
  20. point state[][];
  21. const int cx[] = {, , , -};
  22. const int cy[] = {, -, , };
  23. int main(){
  24. ios::sync_with_stdio(false);
  25. memset(state, , sizeof(state));
  26. for(int i = ; i < ; ++i)
  27. for(int j = ; j < ; ++j)
  28. cin >> maze[i][j];
  29. queue<point> q;
  30. q.push({, });
  31. maze[][] = ;
  32. bool flag = false;
  33. while(!q.empty()){
  34. point k = q.front();
  35. q.pop();
  36. for(int i = ; i < ; ++i){
  37. int x = k.x + cx[i], y = k.y + cy[i];
  38. if(x < || x > || y < || y > || maze[x][y]) continue;
  39. q.push({x, y});
  40. maze[x][y] = ;
  41. state[x][y] = k;
  42. if(x == && y == ){
  43. flag = true;
  44. break;
  45. }
  46. }
  47. if(flag) break;
  48. }
  49. point k = {, };
  50. stack<point> s;
  51. while(!(k.x == && k.y == )){
  52. s.push(k);
  53. k = state[k.x][k.y];
  54. }
  55. s.push(point{, });
  56. while(!s.empty()){
  57. cout << "(" << s.top().x << ", " << s.top().y << ")" << endl;
  58. s.pop();
  59. }
  60. return ;
  61. }

L 数水坑类的    8个方向

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. int ans;
  6. char maze[][];
  7. const int cx[] = {, , , , , -, -, -};
  8. const int cy[] = {, -, , -, , , , -};
  9. void dfs(int x, int y){
  10. maze[x][y] = '*';
  11. for(int i = ; i < ; ++i){
  12. int nx = x + cx[i], ny = y + cy[i];
  13. if(maze[nx][ny] == '@') dfs(nx, ny);
  14. }
  15. }
  16. int main(){
  17. ios::sync_with_stdio(false);
  18. int m, n;
  19. while(cin >> m >> n){
  20. if(m == && n == ) break;
  21. ans = ;
  22. for(int i = ; i < m; ++i){
  23. cin.get();
  24. for(int j = ; j < n; ++j)
  25. cin >> maze[i][j];
  26. }
  27. for(int i = ; i < m; ++i){
  28. for(int j = ; j < n; ++j){
  29. if(maze[i][j] == '@'){
  30. ++ans;
  31. dfs(i, j);
  32. }
  33. }
  34. }
  35. cout << ans << endl;
  36. }
  37. return ;
  38. }

M 可乐.....改了好久蜜汁哇    果断重写   简直恶心

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. //#define print() cout<<ts<<" "<<tn<<" "<<tm<<endl;
  4.  
  5. struct state{
  6. int s, n, m;
  7. state(int ss = , int nn = , int mm = ): s(ss), n(nn), m(mm) {}
  8. };
  9. int s, n, m;
  10. queue<state> q;
  11. const int MAXN = ;
  12. int step[MAXN][MAXN][MAXN];
  13.  
  14. int main(){
  15. while(cin >> s >> n >> m){
  16. if(s == && n == && m == ) break;
  17. if(s & ) cout << "NO" << endl;
  18. else{
  19. while(!q.empty()) q.pop();
  20. memset(step, -, sizeof(step));
  21. int ans = -;
  22. q.push(state(s, , ));
  23. step[s][][] = ;
  24. while(!q.empty()){
  25. state p = q.front();
  26. q.pop();
  27. int ss = p.s, nn = p.n, mm = p.m;
  28. if((ss == s/ && nn == s/) || (ss == s/ && mm == s/) || (nn == s/ && mm == s/)){
  29. ans = step[ss][nn][mm];
  30. break;
  31. }
  32. state tmp;
  33. //s -> n || s -> m
  34. if(ss){
  35. //s -> n
  36. if(ss > n - nn){
  37. tmp.s = ss - (n - nn);
  38. tmp.n = n;
  39. tmp.m = mm;
  40. }
  41. else{
  42. tmp.s = ;
  43. tmp.n = nn + ss;
  44. tmp.m = mm;
  45. }
  46. if(step[tmp.s][tmp.n][tmp.m] == -){
  47. step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + ;
  48. q.push(tmp);
  49. }
  50. //ss -> m
  51. if(ss > m - mm){
  52. tmp.s = ss - (m - mm);
  53. tmp.m = m;
  54. tmp.n = nn;
  55. }
  56. else{
  57. tmp.s = ;
  58. tmp.m = mm + ss;
  59. tmp.n = nn;
  60. }
  61. if(step[tmp.s][tmp.n][tmp.m] == -){
  62. step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + ;
  63. q.push(tmp);
  64. }
  65. }
  66. //n -> s || n -> m
  67. if(nn){
  68. //n -> s
  69. if(nn > s - ss){
  70. tmp.n = nn - (s - ss);
  71. tmp.s = s;
  72. tmp.m = mm;
  73. }
  74. else{
  75. tmp.n = ;
  76. tmp.s = ss + nn;
  77. tmp.m = mm;
  78. }
  79. if(step[tmp.s][tmp.n][tmp.m] == -){
  80. step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + ;
  81. q.push(tmp);
  82. }
  83. //n -> m
  84. if(nn > m - mm){
  85. tmp.n = nn - (m - mm);
  86. tmp.m = m;
  87. tmp.s = ss;
  88. }
  89. else{
  90. tmp.n = ;
  91. tmp.m = mm + nn;
  92. tmp.s = ss;
  93. }
  94. if(step[tmp.s][tmp.n][tmp.m] == -){
  95. step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + ;
  96. q.push(tmp);
  97. }
  98. }
  99. //m -> n || m -> s
  100. if(mm){
  101. //m -> n
  102. if(mm > n - nn){
  103. tmp.m = mm - (n - nn);
  104. tmp.n = n;
  105. tmp.s = ss;
  106. }
  107. else{
  108. tmp.m = ;
  109. tmp.n = nn + mm;
  110. tmp.s = ss;
  111. }
  112. if(step[tmp.s][tmp.n][tmp.m] == -){
  113. step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + ;
  114. q.push(tmp);
  115. }
  116. //m -> s
  117. if(mm > s - ss){
  118. tmp.m = mm - (s - ss);
  119. tmp.s = s;
  120. tmp.n = nn;
  121. }
  122. else{
  123. tmp.m = ;
  124. tmp.s = mm + ss;
  125. tmp.n = nn;
  126. }
  127. if(step[tmp.s][tmp.n][tmp.m] == -){
  128. step[tmp.s][tmp.n][tmp.m] = step[ss][nn][mm] + ;
  129. q.push(tmp);
  130. }
  131. }
  132. }
  133. if(ans == -) cout << "NO" << endl;
  134. else cout << ans << endl;
  135. }
  136. }
  137. return ;
  138. }

N 从两个人的位置开始bfs整个图

  1. #include <queue>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <vector>
  6. #include <iostream>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. struct pos{
  11. int x, y;
  12. pos(int xx, int yy): x(xx), y(yy){};
  13. };
  14. const int MAXN = ;
  15. char maze[MAXN][MAXN];
  16. const int cx[] = {, -, , };
  17. const int cy[] = {, , , -};
  18. vector<pos> kfc;
  19. queue<pos> qy; int yx, yy, stepy[MAXN][MAXN];
  20. queue<pos> qm; int mx, my, stepm[MAXN][MAXN];
  21.  
  22. int main(){
  23. int n, m;
  24. while(cin >> n >> m){
  25. kfc.clear();
  26. for(int i = ; i < n; ++i){
  27. cin.get();
  28. for(int j = ; j < m; ++j){
  29. char ch;
  30. cin >> ch;
  31. if(ch == '#') maze[i][j] = ch;
  32. else{
  33. if(ch == 'Y'){
  34. yx = i;
  35. yy = j;
  36. }
  37. if(ch == 'M'){
  38. mx = i;
  39. my = j;
  40. }
  41. if(ch == '@')
  42. kfc.push_back(pos(i, j));
  43. maze[i][j] = '.';
  44. }
  45. }
  46. }
  47. while(!qy.empty()) qy.pop();
  48. while(!qm.empty()) qm.pop();
  49. memset(stepy, -, sizeof(stepy));
  50. memset(stepm, -, sizeof(stepm));
  51. qy.push(pos(yx, yy));
  52. stepy[yx][yy] = ;
  53. while(!qy.empty()){
  54. int x = qy.front().x, y = qy.front().y;
  55. qy.pop();
  56. for(int i = ; i < ; ++i){
  57. int nx = x + cx[i], ny = y + cy[i];
  58. if(nx < || n < nx || ny < || m < ny || maze[nx][ny] == '#' || stepy[nx][ny] != -) continue;
  59. stepy[nx][ny] = stepy[x][y] + ;
  60. qy.push(pos(nx, ny));
  61. }
  62. }
  63. qm.push(pos(mx, my));
  64. stepm[mx][my] = ;
  65. while(!qm.empty()){
  66. int x = qm.front().x, y = qm.front().y;
  67. qm.pop();
  68. for(int i = ; i < ; ++i){
  69. int nx = x + cx[i], ny = y + cy[i];
  70. if(nx < || n < nx || ny < || m < ny || maze[nx][ny] == '#' || stepm[nx][ny] != -) continue;
  71. stepm[nx][ny] = stepm[x][y] + ;
  72. qm.push(pos(nx, ny));
  73. }
  74. }
  75. int ans = 0x7f7f7f7f;
  76. for(vector<pos>::iterator i = kfc.begin(); i != kfc.end(); ++i){
  77. int x = (*i).x, y = (*i).y;
  78. ans = min(ans, stepy[x][y] + stepm[x][y]);
  79. }
  80. cout << ans* << endl;
  81. }
  82. return ;
  83. }

.............................................................................

kuangbin专题一 简单搜索的更多相关文章

  1. [kuangbin带你飞]专题一 简单搜索 题解报告

    又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...

  2. [kuangbin带你飞]专题一 简单搜索(回顾)

    A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...

  3. kuangbin专题 专题一 简单搜索 Oil Deposits HDU - 1241

    题目链接:https://vjudge.net/problem/HDU-1241 题意:问有几个油田,一个油田由相邻的‘@’,组成. 思路:bfs,dfs都可以,只需要遍历地图,遇到‘@’,跑一遍搜索 ...

  4. kuangbin专题 专题一 简单搜索 迷宫问题 POJ - 3984

    题目链接:https://vjudge.net/problem/POJ-3984 这个题目,emm,上代码,看的估计应该是刚开始接触搜索的,我带点注释,你能慢慢理解. #include <ios ...

  5. kuangbin专题 专题一 简单搜索 Fire! UVA - 11624

    题目链接:https://vjudge.net/problem/UVA-11624 题意:一个迷宫,可能有一个或者多个地方着火了,每过1个时间消耗,火会向四周蔓延,问Joe能不能逃出迷宫,只要走出迷宫 ...

  6. kuangbin专题 专题一 简单搜索 Fliptile POJ - 3279

    题目链接:https://vjudge.net/problem/POJ-3279 题意:格子有两面,1表示黑色格子,0表示白色格子,奶牛每次可以踩一个格子,踩到的格子和它周围的上下左右格子都会翻面,也 ...

  7. kuangbin专题 专题一 简单搜索 Shuffle'm Up POJ - 3087

    题意:(1)有两副颜色多样的扑克牌,(A~H)表示不同颜色,给你两副牌,S1,S2和一副你需要洗出的KEY,S12由S2最底部,S1底部...一直下去,直到洗成S12,就是图片展示的那样.(2)洗好的 ...

  8. kuangbin专题 专题一 简单搜索 Prime Path POJ - 3126

    题目链接:https://vjudge.net/problem/POJ-3126 题意:给你两个四位的素数N,M,每次改变N四位数中的其中一位,如果能经过有限次数的替换变成四位数M,那么求出最少替换次 ...

  9. kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251

    题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...

随机推荐

  1. JavaScript修改Canvas图片

    用JavaScript修改Canvas图片的分辨率(DPI)   应用场景: 仓库每次发货需要打印标签, Canvas根据从数据库读取的产品信息可以生成标签JPG, 但是这个JPG图片的默认分辨率(D ...

  2. 街景地图 API

    SOSO街景地图 API (Javascript)开发教程(1)- 街景   SOSO街景地图 Javascript API 干什么用的? 你想在网页里嵌入个地图,就需要它了! 另外,它还支持:地点搜 ...

  3. HTTP状态码的意义

    100系列码 从100到199范围的HTTP状态码是信息报告码.基于各种原因考虑,大多数情况下我们是 很少看见这些代码的.首先,如果一个浏览器尝试访问一个网站,而网站返回这些代码时,它们往往都不会显示 ...

  4. ASP.NET MVC4简单使用ELMAH记录系统日志

    ASP.NET MVC4简单使用ELMAH记录系统日志 前言 在项目开发.测试以及已经上线的项目中都会存在bug,而如果我们在项目的各个阶段都能及时的监控系统出现的任何问题,那么对于我们开发人员来说完 ...

  5. .net基础收集

    .net基础收集 最近的面试让我知道基础知识的重要性,而我也每天都在网上找一些基础题来看.其实面试无非都是一些理论基础,只有基础过关了,才会被问到技术性的问题,所以第一关一定要打好.下面是我收集的一些 ...

  6. 一步一步深入spring(7)-- 整合spring和JDBC的环境

    1.配置数据源 (1).添加支持数据源的jar包commons-dbcp.jar .commons-pool.jar 当然也要添加其他的spring用到的jar以及这里用到的数据库mysql的jar ...

  7. CKEditor 自主控制图片上传

    在ASP.NET中使用CKEditor编辑器,如果想控制图片上传,即把上传的图片路径名存到数据中,可以自定义一个上传功能 首先自定义CKEditor的配置文件 在config.js中添加以下代码,红色 ...

  8. [置顶] “河软CSDN2011级表彰暨实习动员大会”顺利召开!

    9点30分 伴随着激昂的开场曲,主持人走到台前!“河软CSDN2011级表彰暨 实习动员大会即将开始,请各位嘉宾入场!”他们分别是“CSDN教育事业部总经 理李天山先生”“河北软件职业技术学院 软件工 ...

  9. 使用MFC CImage类绘制PNG图片时遇到的问题

    为了测试CImage绘制PNG图片的效果,我们用截图软件截得一张360的界面,然后使用PhotoShop等工具在图片的周边加上了透明的区域,然后保存成PNG图片文件.CImage首先从文件中加载,即 ...

  10. CCNA网络工程师学习进程(7)路由器的路由配置

        前面一节已经介绍了路由器的端口配置,接着我们介绍路由器的路由配置:静态路由.默认路由和浮动路由的配置:动态路由协议的配置,包括RIP.IGRP.EIGRP和OSPF.     (1)路由器的基 ...