fork(1) download
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <queue>
  5. #include <utility>
  6. using namespace std;
  7.  
  8. int cost[501][501],xx,yy,n,m;
  9. char mat[501][501];
  10. bool visit[501][501],first = true;
  11.  
  12. int a[] = {-1,0,0,1}, b[] = {0,-1,1,0};
  13.  
  14. void check(int x,int y,int level) {
  15. visit[x][y] = true;
  16. cost[x][y] = level;
  17. for(int i = 0; i < 4; ++i) {
  18. xx = x + a[i];
  19. yy = y + b[i];
  20. if(0 <= xx and xx < n and 0 <= yy and yy < m and mat[xx][yy] == '.') {
  21. if(level + 1 < cost[xx][yy] or !visit[xx][yy]) check(xx,yy,level + 1);
  22. }
  23. }
  24. }
  25.  
  26. int max() {
  27. int r = -1;
  28. for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(mat[i][j] == '.') r = max(r,cost[i][j]);
  29. return r;
  30. }
  31.  
  32. void show() {
  33. if(!first) puts("---");
  34. int r = max();
  35. for(int i = 0; i < n; ++i) {
  36. for(int j = 0; j < m; ++j) {
  37. if(cost[i][j] == r) printf("X");
  38. else printf("%c",mat[i][j]);
  39. }
  40. puts("");
  41. }
  42. }
  43.  
  44. int main() {
  45. while(scanf("%d %d",&n,&m) == 2) {
  46. queue<pair<int,int> > cola;
  47. for(int i = 0; i < n; ++i) {
  48. scanf("\n");
  49. for(int j = 0; j < m; ++j) {
  50. scanf("%c",&mat[i][j]);
  51. if(mat[i][j] == 'V') cola.push(make_pair(i,j));
  52. }
  53. }
  54. memset(cost,-1,sizeof cost);
  55. memset(visit,0,sizeof visit);
  56. while(!cola.empty()) {
  57. pair<int,int> aux = cola.front();
  58. check(aux.first, aux.second,0);
  59. cola.pop();
  60. }
  61. show();
  62. first = false;
  63. }
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.02s 4288KB
stdin
6 6
......
......
.V....
......
......
.....V

3 10
...#......
...#..#.V.
.V....#...

3 10
V###...V..
...#V.#.V.
.V.V.V#...

3 10
V###.V.V.V
..##V.#.V.
.V.V.V##.V
6 6
VVVVVV
V....V
V....V
V....V
V....V
VVVVVV

6 5
VVVVV
V...V
V...V
V...V
V...V
VVVVV

5 6
......
......
...V..
......
......

5 6
VVVVVV
VVVV.V
V.VV.V
V.##.V
VVVVVV

5 6
VVVVVV
VVV.VV
VV...V
VVV.VV
VVVVVV
4 6
..#...
#.#V..
.V####
V.V...

9 9
.........
.#######.
.#.....#.
.#.###.#.
V#.#V#.#.
.#.###.#V
.#V....#.
.#######.
....V....
9 23
..#..........#.V.V.....
....#.#........V...V##.
.#..V....##V.#.#V......
...V...#.#.V..#.....#..
....#V.V.#.............
..##.V.............#...
.....#.#...#.#.#.#.....
......V....#.........V#
....V...#.............#

8 35
..VV.....#.........VV#.......V..#..
....V.............V.....V......V.V.
.......V.............#........V...V
...#.....V..V........V.............
....#.#.....V#...............V....V
.#...V.........................V#..
..........#........................
#.......#...#V......#......#..V....
stdout
....XX
......
.V....
......
......
.....V
---
...#X.....
...#.X#.V.
.V....#...
---
V###.X.V.X
..X#V.#.V.
.V.V.V#X.X
---
V###XVXVXV
XX##VX#XVX
XVXVXV##XV
---
VVVVVV
V....V
V.XX.V
V.XX.V
V....V
VVVVVV
---
VVVVV
V...V
V.X.V
V.X.V
V...V
VVVVV
---
X.....
......
...V..
......
X.....
---
VVVVVV
VVVVXV
VXVVXV
VX##XV
VVVVVV
---
VVVVVV
VVV.VV
VV.X.V
VVV.VV
VVVVVV
---
X.#..X
#.#V..
.V####
V.V..X
---
....XX...
.#######.
.#....X#.
.#.###.#.
V#.#V#.#.
.#.###.#V
.#V....#.
.#######.
....V....
---
..#..........#.V.V.....
....#.#........V...V##.
.#..V....##V.#.#V......
...V...#.#.V..#.....#..
....#V.V.#.............
..##.V.............#...
.....#.#...#.#.#.#.....
......V....#.........V#
....V...#.....X.......#
---
..VV.....#.........VV#.......V..#..
....V.............V.....V......V.V.
.......V.............#........V...V
...#.....V..V........V.............
....#.#.....V#...............V....V
X#...V.........................V#..
..........#........................
#.......#...#V......#....X.#..V....