fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. int arr[22][22];
  8. bool vis[22][22];
  9.  
  10. int dy[4]= {-1,-1, 0, 1};
  11. int dx[4]= {0 ,1 , 1, 1};
  12. // 위, 윗대각, 우측, 아랫대각 순
  13.  
  14. void func(int y,int x, int st_y, int st_x,int num,int dir){ // 현재 좌표, 시작 좌표, 카운트 개수, 방향 순으로 의미
  15. vis[y][x] = true;
  16.  
  17. for(int i=0; i<4; i++){
  18. int ny = y + dy[i];
  19. int nx = x + dx[i];
  20.  
  21. if(ny<19 && ny>=0 && nx<19 && nx>=0 && !vis[ny][nx]&& arr[ny][nx] ==arr[y][x]){
  22.  
  23. if(x==nx){ //위
  24. if(dir==1){
  25. func(ny,nx,st_y,st_x,num+1,dir);
  26. }
  27. else func(ny,nx,st_y,st_x,2,1);
  28. }
  29. else if(y-1==ny && x +1 ==nx ){ //윗대각
  30. if(dir==2){
  31. func(ny,nx,st_y,st_x,num+1,dir);
  32. }
  33. else func(ny,nx,st_y,st_x,2,2);
  34. }
  35. else if(y== ny){ //우측
  36. if(dir==3){
  37. func(ny,nx,st_y,st_x,num+1,dir);
  38. }
  39. else func(ny,nx,st_y,st_x,2,3);
  40. }
  41. else {// 아래 대각
  42. if(dir==4){
  43. func(ny,nx,st_y,st_x,num+1,dir);
  44. }
  45. else func(ny,nx,st_y,st_x,2,4);
  46.  
  47. }
  48. }
  49. }
  50.  
  51. vis[y][x] = false;
  52.  
  53. if(num ==5){
  54. if(dir==1){
  55. if( ( y-1>=0 && arr[y][x] == arr[y-1][x]) ||
  56. (y+5<19 && arr[y][x] == arr[y+5][x]) ){
  57. return;
  58. }
  59. }
  60.  
  61. else if(dir==2){
  62. if( (y-1>0&& x+1<19 && arr[y][x]== arr[y-1][x+1])||
  63. (y+5<19 && x-5>=0 && arr[y][x] == arr[y+5][x-5])){
  64. return ;
  65. }
  66. }
  67. else if(dir==3){
  68. if( (x+1<19 && arr[y][x] == arr[y][x+1]) ||
  69. (x-5>=0 && arr[y][x] == arr[y][x-5]) ){
  70. return ;
  71. }
  72. }
  73.  
  74. else{
  75. if((x+1<19 && y+1<19 && arr[y][x] ==arr[y+1][x+1]) ||
  76. (x-5>=0 && y-5>=0 && arr[y][x] == arr[y-5][x-5]) ){
  77. return ;
  78. }
  79. }
  80.  
  81. cout<< arr[y][x]<<"\n"<<st_y+1<<" "<<st_x+1<<"\n";
  82. exit(0);
  83. }
  84.  
  85.  
  86.  
  87.  
  88. }
  89.  
  90. int main(){
  91.  
  92. cin.tie(0);
  93. ios::sync_with_stdio(0);
  94.  
  95. for(int i=0; i<19; i++){
  96. for(int j=0; j<19; j++){
  97. cin >>arr[i][j];
  98. }
  99. }
  100.  
  101. for(int i=0; i<19; i++){
  102. for(int j=0; j<19; j++){
  103. if( (arr[i][j] ==1 || arr[i][j] ==2) && !vis[i][j]) {
  104. func(i,j,i,j,1,0);
  105. }
  106. }
  107. }
  108.  
  109. return 0;
  110.  
  111.  
  112.  
  113.  
  114.  
  115. }
Success #stdin #stdout 0.01s 5460KB
stdin
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 2 2 2 1 0 0 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 2 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 2 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
stdout
1
7 4