Maruf_Hasan

crsytal maze by mustafiz

Feb 2nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define mp make_pair
  4.  
  5. using namespace std;
  6.  
  7. #define ll long long int
  8.  
  9. const int maxn=501;
  10.  
  11. bool visited[maxn][maxn];
  12.  
  13.  
  14.  
  15. int x[4]= {-1,0,0,1};
  16.  
  17. int y[4]= {0,-1,1,0};
  18.  
  19. int mat[501][501];
  20.  
  21. int R,C,cnt=0;
  22.  
  23. int mark[maxn][maxn];
  24.  
  25. void clr()
  26.  
  27. {
  28.  
  29.  
  30.  
  31. //memset(cost, 0, sizeof(cost));
  32.  
  33. memset(visited, 0, sizeof(visited));
  34.  
  35.  
  36.  
  37.  
  38.  
  39. }
  40.  
  41.  
  42.  
  43. int bfs(int s,int e)
  44.  
  45.  
  46.  
  47. {
  48.  
  49. clr();
  50.  
  51. cnt=0;
  52.  
  53. queue<int>q;
  54.  
  55. q.push(s);
  56.  
  57. q.push(e);
  58.  
  59. visited[s][e]=1;
  60.  
  61. if(mat[s][e]==1)
  62.  
  63. cnt++;
  64.  
  65. // cost[s][e]=0;
  66.  
  67.  
  68.  
  69. while(!q.empty())
  70.  
  71. {
  72.  
  73. int ux,uy;
  74.  
  75. ux=q.front();
  76.  
  77. q.pop();
  78.  
  79. uy=q.front();
  80.  
  81. q.pop();
  82.  
  83.  
  84.  
  85. for(int k=0; k<4; k++)
  86.  
  87. {
  88.  
  89. int vx=ux+x[k];
  90.  
  91. int vy=uy+y[k];
  92.  
  93.  
  94.  
  95.  
  96.  
  97. if((vx>=1 && vx<=R) && ((vy>=1 && vy<=C ) && visited[vx][vy]==0))
  98.  
  99. {
  100.  
  101. //cout<<ux<<" "<<uy<<" "<<x[k]<<" "<<y[k]<<endl;
  102.  
  103. // cout<<" "<<vx<<" "<<vy<<endl;
  104.  
  105. if(mat[vx][vy]==-1)
  106.  
  107. continue;
  108.  
  109. if(mat[vx][vy]==1)
  110.  
  111. cnt++;
  112.  
  113. //cost[vx][vy]=cost[ux][uy]+1;
  114.  
  115. visited[vx][vy]=1;
  116.  
  117. q.push(vx);
  118.  
  119. q.push(vy);
  120.  
  121.  
  122.  
  123.  
  124.  
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. }
  132.  
  133. }
  134.  
  135.  
  136.  
  137. return cnt;
  138.  
  139.  
  140.  
  141. }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. int main()
  150.  
  151. {
  152.  
  153.  
  154.  
  155. int n, t, p,qq,m;
  156.  
  157. scanf("%d", &t);
  158.  
  159. for(int ch=1; ch<=t; ch++)
  160.  
  161. {
  162.  
  163. clr();
  164.  
  165. memset(mark,0,sizeof(mark));
  166.  
  167.  
  168.  
  169. int i,j;
  170.  
  171. scanf("%d%d%d",&m,&n,&qq);
  172.  
  173. R=m;
  174.  
  175. C=n;
  176.  
  177. for(i=1; i<=m; i++)
  178.  
  179. {
  180.  
  181.  
  182.  
  183. for(j=0; j<n; j++)
  184.  
  185. {
  186.  
  187. char c;
  188.  
  189. scanf(" %c", &c);
  190.  
  191. if(c=='.')
  192.  
  193. {
  194.  
  195. // cout<<c<<"ydfv "<<endl;
  196.  
  197. mat[i][j+1]=0;
  198.  
  199. }
  200.  
  201. else if(c=='#')
  202.  
  203. {
  204.  
  205. mat[i][j+1]=-1;
  206.  
  207. }
  208.  
  209. else if(c=='C')
  210.  
  211. {
  212.  
  213. mat[i][j+1]=1;
  214.  
  215. }
  216.  
  217.  
  218.  
  219. }
  220.  
  221. }
  222.  
  223. printf("Case %d:\n",ch);
  224.  
  225. // printf("Case luiwreyg ",ch);
  226.  
  227. int sx,sy,py,mm,nm;
  228.  
  229. for(int ii=0; ii<qq; ii++)
  230.  
  231. {
  232.  
  233.  
  234.  
  235.  
  236.  
  237. scanf("%d %d", &mm,&nm);
  238.  
  239. if(mark[mm][nm])
  240.  
  241. {
  242.  
  243. py=mark[mm][nm];
  244.  
  245. //kjdf;
  246.  
  247. }
  248.  
  249.  
  250.  
  251. else
  252.  
  253. {
  254.  
  255. clr();
  256.  
  257. py=bfs(mm,nm);
  258.  
  259.  
  260.  
  261. for(i=1; i<=m; i++)
  262.  
  263. {
  264.  
  265. for(j=1; j<=n; j++)
  266.  
  267. {
  268.  
  269. if(visited[i][j])
  270.  
  271. mark[i][j]=py;
  272.  
  273. }
  274.  
  275. }
  276.  
  277. }
  278.  
  279.  
  280.  
  281.  
  282.  
  283. printf("%d\n", py);
  284.  
  285. }
  286.  
  287.  
  288.  
  289.  
  290.  
  291. }
  292.  
  293.  
  294.  
  295. return 0;
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment