zoj 1047

缺乏、安全感 2022-05-31 12:30 295阅读 0赞

Technicians in a pathology lab analyze digitized images of slides. Objects on a slide are selected for analysis by a mouse click on the object. The perimeter of the boundary of an object is one useful measure. Your task is to determine this perimeter for selected objects.

The digitized slides will be represented by a rectangular grid of periods, ‘.’, indicating empty space, and the capital letter ‘X’, indicating part of an object. Simple examples are

  1. XX Grid 1 .XXX Grid 2
  2. XX .XXX
  3. .XXX
  4. ...X
  5. ..X.
  6. X...

An X in a grid square indicates that the entire grid square, including its boundaries, lies in some object. The X in the center of the grid below is adjacent to the X in any of the 8 positions around it. The grid squares for any two adjacent X’s overlap on an edge or corner, so they are connected.

  1. XXX
  2. XXX Central X and adjacent X's
  3. XXX

An object consists of the grid squares of all X’s that can be linked to one another through a sequence of adjacent X’s. In Grid 1, the whole grid is filled by one object. In Grid 2 there are two objects. One object contains only the lower left grid square. The remaining X’s belong to the other object.

The technician will always click on an X, selecting the object containing that X. The coordinates of the click are recorded. Rows and columns are numbered starting from 1 in the upper left hand corner. The technician could select the object in Grid 1 by clicking on row 2 and column 2. The larger object in Grid 2 could be selected by clicking on row 2, column 3. The click could not be on row 4, column 3.
2cc7750d061ca72a0525e61ea830b888_v_1519284786
One useful statistic is the perimeter of the object. Assume each X corresponds to a square one unit on each side. Hence the object in Grid 1 has perimeter 8 (2 on each of four sides). The perimeter for the larger object in Grid 2 is illustrated in the figure at the left. The length is 18.

Objects will not contain any totally enclosed holes, so the leftmost grid patterns shown below could NOT appear. The variations on the right could appear:

  1. Impossible Possible
  2. XXXX XXXX XXXX XXXX
  3. X..X XXXX X... X...
  4. XX.X XXXX XX.X XX.X
  5. XXXX XXXX XXXX XX.X
  6. ..... ..... ..... .....
  7. ..X.. ..X.. ..X.. ..X..
  8. .X.X. .XXX. .X... .....
  9. ..X.. ..X.. ..X.. ..X..
  10. ..... ..... ..... .....

Input

The input will contain one or more grids. Each grid is preceded by a line containing the number of rows and columns in the grid and the row and column of the mouse click. All numbers are in the range 1-20. The rows of the grid follow, starting on the next line, consisting of ‘.’ and ‘X’ characters.

The end of the input is indicated by a line containing four zeros. The numbers on any one line are separated by blanks. The grid rows contain no blanks.

Output

For each grid in the input, the output contains a single line with the perimeter of the specified object.

Sample Input

  1. 2 2 2 2
  2. XX
  3. XX
  4. 6 4 2 3
  5. .XXX
  6. .XXX
  7. .XXX
  8. ...X
  9. ..X.
  10. X...
  11. 5 6 1 3
  12. .XXXX.
  13. X....X
  14. ..XX.X
  15. .X...X
  16. ..XXX.
  17. 7 7 2 6
  18. XXXXXXX
  19. XX...XX
  20. X..X..X
  21. X..X...
  22. X..X..X
  23. X.....X
  24. XXXXXXX
  25. 7 7 4 4
  26. XXXXXXX
  27. XX...XX
  28. X..X..X
  29. X..X...
  30. X..X..X
  31. X.....X
  32. XXXXXXX
  33. 0 0 0 0

Sample Output

  1. 8
  2. 18
  3. 40
  4. 48
  5. 8

题目解析:

这个题目就是一个简单的dfs模板题目,我是将这个图的周围都用’.’圈起来了,只需要判断每一个’X’周围有几个’.’就是答案

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. using namespace std;
  5. const int maxn = 1000+100;
  6. char mapp[maxn][maxn];
  7. int vis[maxn][maxn];
  8. int dir[8][2]= {
  9. {1,0},{-1,0},{0,1},{0,-1},{1,-1},{1,1},{-1,1},{-1,-1}};
  10. int arr[4][2]= {
  11. {0,1},{0,-1},{-1,0},{1,0}};
  12. int ans1,ans2;
  13. int ans;
  14. int a,b,c,d;
  15. int cnt;
  16. void dfs(int x,int y)
  17. {
  18. int r,c;
  19. mapp[x][y] = '#';
  20. for(int i=0; i<4; i++)
  21. {
  22. r = x+arr[i][0];
  23. c = y+arr[i][1];
  24. if(mapp[r][c]=='.')
  25. cnt++;
  26. }
  27. for(int i=0; i<8; i++)
  28. {
  29. r = x+dir[i][0];
  30. c = y+dir[i][1];
  31. if(mapp[r][c]=='X')
  32. dfs(r,c);
  33. }
  34. }
  35. int main()
  36. {
  37. //freopen("in.txt","r",stdin);
  38. int i,x,y;
  39. while(cin>>a>>b>>c>>d)
  40. {
  41. memset(mapp,'.',sizeof(mapp));
  42. cnt = 0;
  43. if(a+b+c+d==0)
  44. break;
  45. for(i=1; i<=a; i++)
  46. {
  47. for(int j=1; j<=b; j++)
  48. cin>>mapp[i][j];
  49. mapp[i][b+1] = '.';
  50. }
  51. dfs(c,d);
  52. cout<<cnt<<endl;
  53. }
  54. return 0;
  55. }

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读

    相关 1047. 编程团体赛(20)

    编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。 现给定所有队员的比赛成绩,请你编写程序找出冠军队。 输入

    相关 ZOJ 3941

    题意:有n(10)段时间,会举行party,每个party有开始时间,结束时间,不同party举行时间可能重复。(时间范围为1~1e9) 我们一共最多可以参加m(1e9)