fork download
  1. //
  2. // main.cpp
  3. // Codeforces
  4. //
  5. // Created by Kartik Satoskar on 15/04/20.
  6. // Copyright © 2020 Personal. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. #include <algorithm>
  13. #include <sstream>
  14. #include <queue>
  15. #include <deque>
  16. #include <bitset>
  17. #include <iterator>
  18. #include <list>
  19. #include <stack>
  20. #include <map>
  21. #include <set>
  22. #include <functional>
  23. #include <numeric>
  24. #include <utility>
  25. #include <limits>
  26. #include <time.h>
  27. #include <math.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <assert.h>
  32. #define ll long long
  33. using namespace std;
  34.  
  35. vector<string> graph;
  36. bool flg0, flg1;
  37.  
  38. int rows[4] = {-1,1,0,0};
  39. int cols[4] = {0,0,1,-1};
  40.  
  41. void dfs0(int i, int j) {
  42. if(graph[i][j] == 'F') {
  43. flg0 = true;
  44. return;
  45. }
  46. for(int d = 0;d<4;d++) {
  47. int u = rows[d] + i;
  48. int v = cols[d] + j;
  49. if(u>=0 && u<graph.size() && v>=0 && v<graph.size() && graph[u][v] != '#') {
  50. if(graph[u][v] != 'F')
  51. graph[u][v] = '#';
  52. dfs0(u,v);
  53. }
  54. }
  55. }
  56.  
  57. void dfs1(int i, int j) {
  58. if(graph[i][j] == 'F') {
  59. flg1 = true;
  60. return;
  61. }
  62. for(int d = 0;d<4;d++) {
  63. int u = rows[d] + i;
  64. int v = cols[d] + j;
  65. if(u>=0 && u<graph.size() && v>=0 && v<graph.size() && graph[u][v] != '#') {
  66. if(graph[u][v] != 'F')
  67. graph[u][v] = '#';
  68. dfs1(u,v);
  69. }
  70. }
  71. }
  72.  
  73. int main(int argc, const char * argv[]) {
  74. ll t,n,x;
  75. string s;
  76. cin >> t;
  77. while (t--) {
  78. flg0 = false;
  79. flg1 = false;
  80. graph.clear();
  81. cin >> n;
  82. x = n;
  83. while(n--) {
  84. cin >> s;
  85. graph.push_back(s);
  86. }
  87. dfs0(0, 0);
  88. dfs1(0,0);
  89. if(!flg0 && !flg1) {
  90. cout << "0\n";
  91. continue;
  92. }
  93. if(flg0) {
  94. if(graph[x-2][x-1] == '0') {
  95. cout << x-1 << " " << x << "\n";
  96. }
  97.  
  98. if(graph[x-1][x-2] == '0') {
  99. cout << x << " " << x-1 << "\n";
  100. }
  101. }
  102.  
  103. if(flg1) {
  104. if(graph[x-2][x-1] == '1') {
  105. cout << x-1 << " " << x << "\n";
  106. }
  107.  
  108. if(graph[x-1][x-2] == '1') {
  109. cout << x << " " << x-1 << "\n";
  110. }
  111. }
  112. }
  113. return 0;
  114. }
  115.  
Success #stdin #stdout 0s 4252KB
stdin
3
4
S010
0001
1000
111F
3
S10
101
01F
5
S0101
00000
01111
11111
0001F
stdout
Standard output is empty