Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<string>
  4. #include<algorithm>
  5. #include<cstdio>
  6. #include<cstring>
  7. #include<vector>
  8. #include<stack>
  9. #include<queue>
  10. #include<map>
  11. #define FRU freopen("out.txt","w",stdout)
  12. #define FRO freopen("in.txt","r",stdin)
  13. #define pb push_back
  14. const int row[]= {-1,0,1, 0,-1,-1,1, 1}; // Kings Move
  15. const int col[]= {0,1,0,-1,-1, 1,1,-1}; // Kings Move
  16. const int row1[]= {-2, -2, -1, -1, 0, 1, 1, 2, 2}; // Knights Move
  17. const int col1[]= {-1, 1, -2, 2, 0, -2, 2, -1, 1}; // Knights Move
  18. //const int row[]={-1,0,0,1,0};
  19. //const int col[]={0,-1,1,0,0};
  20.  
  21.  
  22. using namespace std;
  23. int grid[201][201],dis[201][201],limit,limit1;
  24. struct data
  25. {
  26. int rw,cl;
  27. };
  28.  
  29. bool vis[201][201];
  30. void bfs(int r,int c)
  31. {
  32.  
  33. for(int i=0; i<201; i++)for(int j=0; j<201; j++)
  34. {
  35. vis[i][j]=0;
  36. dis[i][j]=-1;
  37. }
  38. queue<data>q;
  39. data u,v;
  40. u.rw=r;
  41. u.cl=c;
  42. vis[r][c]=1;
  43. dis[r][c]=0;
  44. q.push(u);
  45. while(!q.empty())
  46. {
  47. u=q.front();
  48. q.pop();
  49. for(int i=0; i<8; i++)
  50. {
  51. v.rw=u.rw+row[i];
  52. v.cl=u.cl+col[i];
  53. if(vis[v.rw][v.cl]==0&& v.rw>=0&& v.rw<limit&& v.cl>=0&& v.cl<limit1&& grid[v.rw][v.cl]==0)
  54. {
  55. q.push(v);
  56. vis[v.rw][v.cl]=1;
  57. dis[v.rw][v.cl]=dis[u.rw][u.cl]+1;
  58. }
  59. }
  60. }
  61. }
  62.  
  63. int main()
  64. {
  65. //FRO;
  66. //FRU;
  67. int a,b,i,j,k,tc,t;
  68. int n,m,cnt=0,u,v,R,C,r,c;
  69. char ch,s[201][201];
  70. scanf("%d",&tc);
  71. for(t=1; t<=tc; t++)
  72. {
  73. for(i=0; i<201; i++)
  74. {
  75. memset(grid[i],0,200);
  76.  
  77. }
  78. scanf("%d%d",&R,&C);
  79. limit=R;
  80. limit1=C;
  81. for(i=0; i<limit; i++)
  82. {
  83. scanf("%s",s[i]);
  84.  
  85. }
  86. for(i=0; i<limit; i++)
  87. {
  88. for(j=0; j<limit1; j++)
  89. {
  90. ch=s[i][j];
  91. if(ch=='Z')
  92. {
  93. for(k=0; k<9; k++)
  94. {
  95. a=i+row1[k];
  96. b=j+col1[k];
  97. if(a>=0&& b>=0&& a<limit&& b<limit1)
  98. {
  99. grid[a][b]=1;
  100. }
  101. }
  102. }
  103. else if(ch=='A')
  104. {
  105. u=i;
  106. v=j;
  107. }
  108. else if(ch=='B')
  109. {
  110. n=i;
  111. m=j;
  112. }
  113. }
  114. }
  115. grid[u][v]=0;
  116. grid[n][m]=0;
  117. bfs(u,v);
  118.  
  119. if(dis[n][m]==-1)printf("King Peter, you can't go now!\n");
  120. else printf("Minimal possible length of a trip is %d\n",dis[n][m]);
  121. }
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement