Advertisement
juanjo12x

UVA 639_Don't_Get_Rooked

Apr 28th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX 4
  5.  
  6. int max(int value1, int value2){
  7.     if (value1>value2) return value1;
  8.     return value2;
  9. }
  10.  
  11. void reset_board(int board[MAX][MAX]){
  12.     int i,j;
  13.     for (i=0;i<MAX;i++)
  14.         for (j=0;j<MAX;j++)
  15.             board[i][j] = 0;
  16. }
  17.  
  18. void fill_board(int board[MAX][MAX], int board_size){
  19.     int i=0, j;
  20.     char c;
  21.     while (i<board_size){
  22.         j= 0;
  23.         while (j<board_size){
  24.             c= getchar();
  25.             if (c == '\n')
  26.                 continue;
  27.             if (c=='X')
  28.                 board[i][j] = -1;
  29.             j++;
  30.         }
  31.         i++;
  32.     }
  33. }
  34.  
  35. void get_next(int x, int y, int board_size, int* next_x, int* next_y){
  36.     *next_x = x+1;
  37.     *next_y = y;
  38.     if (*next_x == board_size){
  39.         *next_x = 0;
  40.         *next_y = y+1;
  41.     }
  42.    
  43. }
  44.  
  45. int is_legal(int x, int y, int board[MAX][MAX], int board_size){
  46.     if ((board[x][y] == -1) || (board[x][y] == 1))
  47.         return 0;
  48.    
  49.     int i;
  50.     for (i=x-1; i>=0 && board[i][y] != -1; i--){
  51.         if (board[i][y] == 1)
  52.             return 0;
  53.     }
  54.    
  55.     int j;
  56.     for (j=y-1;j>=0 && board[x][j] != -1; j--){
  57.         if (board[x][j] == 1)
  58.             return 0;
  59.     }
  60.            
  61.     return 1;
  62. }
  63.  
  64. void solve_DGR(int x, int y, int board[MAX][MAX], int board_size, int* current_max, int* overall_max){
  65.     if (y==board_size){
  66.         *overall_max = max(*current_max, *overall_max);
  67.         return;
  68.     }
  69.     int next_x, next_y;
  70.     get_next(x, y, board_size, &next_x, &next_y);
  71.     if (is_legal(x, y, board, board_size)){
  72.         board[x][y] = 1;
  73.         (*current_max)++;
  74.         solve_DGR(next_x, next_y, board, board_size, current_max, overall_max);
  75.         board[x][y] = 0; /*BACKTRACK*/
  76.         (*current_max)--;
  77.     }
  78.     solve_DGR(next_x, next_y, board, board_size, current_max, overall_max);
  79.    
  80.     return;
  81. }
  82.  
  83. int main(int argc, char** argv) {
  84.     int board_size;
  85.     int board[MAX][MAX];
  86.     int current_max;
  87.     int overall_max;
  88.    
  89.     scanf("%d", &board_size);
  90.     while (board_size >0){
  91.         current_max = 0;
  92.         overall_max = 0;
  93.         reset_board(board);
  94.         fill_board(board, board_size);
  95.         solve_DGR(0,0, board, board_size, &current_max, &overall_max);
  96.         printf("%d\n", overall_max);
  97.         scanf("%d", &board_size);
  98.     }
  99.    
  100.     return (EXIT_SUCCESS);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement