Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <algorithm>
  7. using namespace std;
  8. int w,h,x,y;
  9. char ch[25][25];
  10. int vis[25][25];
  11. int n=0;
  12.  
  13. void dfs(int y, int x)
  14. {
  15.     int l[]={0,-1,0,1};
  16.     int m[]={1,0,-1,0};
  17.     for(int i=0;i<4;i++)
  18.     {
  19.  
  20.         if((x+m[i])>=0 && (x+m[i])<w && (y+l[i])>=0 && (y+l[i])<h && vis[y+l[i]][x+m[i]]==0 && ch[y+l[i]][x+m[i]]=='.')
  21.         {
  22.             vis[y+l[i]][x+m[i]]=1;
  23.  
  24.         n++;
  25.  
  26.           dfs(y+l[i],x+m[i]);
  27.         }
  28.     }
  29. };
  30. int main()
  31. {
  32.  
  33.         int tc;cin>>tc;
  34.         for(int k=1;k<=tc;k++){
  35.                 n=0;
  36.         scanf("%d %d", &w, &h);    //w and x=number of cols, h and y=number of rows
  37.         for(int p=0;p<=24;p++)
  38.             for(int q=0;q<=24;q++)
  39.                 vis[p][q]=0;
  40.  
  41.  
  42.        for (int i = 0; i < h; i++){
  43.             for (int j = 0; j < w; j++)
  44. {
  45.  
  46.  
  47.                 scanf("%c", &ch[i][j]);
  48.                 if(ch[i][j] == '@') {
  49.                     y = i;
  50.                     x = j;
  51.                     n = 1;
  52.  
  53.                 }
  54.  
  55.  
  56.  
  57.         }getchar();vis[y][x]=1;
  58. }
  59.  
  60.         dfs(y,x);
  61.  
  62. cout<<"Case "<<k<<": "<<n<<endl;
  63.  
  64.  
  65.         }
  66.      return 0;
  67. }
  68.  
  69. /* 1
  70. 11 9
  71.  
  72. .#.........
  73.  
  74. .#.#######.
  75.  
  76. .#.#.....#.
  77.  
  78. .#.#.###.#.
  79.  
  80. .#.#..@#.#.
  81.  
  82. .#.#####.#.
  83.  
  84. .#.......#.
  85.  
  86. .#########.
  87.  
  88. ...........
  89.  
  90. Output: 59 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement