audreych

12615 - Knight Search

Feb 20th, 2021
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. char boards[105][105];
  4. int locRow[105], locCol[105];
  5. char goal[11] = "ICPCASIASG";
  6. int flag2 = 0;
  7. //maybe i dont need to make the array?
  8. void knight(int k, int row, int col, int N){
  9.     if(k >= 10){
  10.         flag2 = 1;
  11.         return;
  12.     } else {
  13.         if(boards[row-2][col-1] == goal[k] && row - 2 >= 1 && col - 1 >= 1) knight(k+1, row-2, col-1, N);
  14.         if(boards[row-2][col+1] == goal[k] && row - 2 >= 1 && col + 1 <= N) knight(k+1, row-2, col+1, N);
  15.         if(boards[row-1][col+2] == goal[k] && row - 1 >= 1 && col + 2 <= N) knight(k+1, row-1, col+2, N);
  16.         if(boards[row+1][col+2] == goal[k] && row + 1 <= N && col + 2 <= N) knight(k+1, row+1, col+2, N);
  17.         if(boards[row+2][col+1] == goal[k] && row + 2 <= N && col + 1 <= N) knight(k+1, row+2, col+1, N);
  18.         if(boards[row+2][col-1] == goal[k] && row + 2 <= N && col - 1 >= 1) knight(k+1, row+2, col-1, N);
  19.         if(boards[row+1][col-2] == goal[k] && row + 1 <= N && col - 2 >= 1) knight(k+1, row+1, col-2, N);
  20.         if(boards[row-1][col-2] == goal[k] && row - 1 >= 1 && col - 2 >= 1) knight(k+1, row-1, col-2, N);
  21.     }
  22.     return;
  23. }
  24.  
  25. int main(){
  26.     int N, len = 10, flag = 1;
  27.     int m = 0, n = 0, cntI = 0;
  28.     char str[10001];
  29.     scanf("%d", &N);
  30.     scanf("%s", str);
  31.     for(int i = 1 ; i <= N; i++){
  32.         for(int j = 1; j <= N; j++){
  33.             boards[i][j] = str[m];
  34.             m++;
  35.             if(boards[i][j] == 'I'){
  36.                 locRow[n] = i;
  37.                 locCol[n] = j;
  38.                 n++;
  39.             }
  40.         }
  41.     }
  42.     cntI = n;
  43.     for(n = 0; n < cntI; n++){
  44.         knight(1, locRow[n], locCol[n], N);
  45.         if(flag2 == 1){
  46.             flag = 1;
  47.             puts("YES");
  48.             break;
  49.         } else {
  50.             flag = 0;
  51.         }
  52.     }
  53.     if(flag == 0) puts("NO");
  54.     return 0;  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment