Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 4
- int max(int value1, int value2){
- if (value1>value2) return value1;
- return value2;
- }
- void reset_board(int board[MAX][MAX]){
- int i,j;
- for (i=0;i<MAX;i++)
- for (j=0;j<MAX;j++)
- board[i][j] = 0;
- }
- void fill_board(int board[MAX][MAX], int board_size){
- int i=0, j;
- char c;
- while (i<board_size){
- j= 0;
- while (j<board_size){
- c= getchar();
- if (c == '\n')
- continue;
- if (c=='X')
- board[i][j] = -1;
- j++;
- }
- i++;
- }
- }
- void get_next(int x, int y, int board_size, int* next_x, int* next_y){
- *next_x = x+1;
- *next_y = y;
- if (*next_x == board_size){
- *next_x = 0;
- *next_y = y+1;
- }
- }
- int is_legal(int x, int y, int board[MAX][MAX], int board_size){
- if ((board[x][y] == -1) || (board[x][y] == 1))
- return 0;
- int i;
- for (i=x-1; i>=0 && board[i][y] != -1; i--){
- if (board[i][y] == 1)
- return 0;
- }
- int j;
- for (j=y-1;j>=0 && board[x][j] != -1; j--){
- if (board[x][j] == 1)
- return 0;
- }
- return 1;
- }
- void solve_DGR(int x, int y, int board[MAX][MAX], int board_size, int* current_max, int* overall_max){
- if (y==board_size){
- *overall_max = max(*current_max, *overall_max);
- return;
- }
- int next_x, next_y;
- get_next(x, y, board_size, &next_x, &next_y);
- if (is_legal(x, y, board, board_size)){
- board[x][y] = 1;
- (*current_max)++;
- solve_DGR(next_x, next_y, board, board_size, current_max, overall_max);
- board[x][y] = 0; /*BACKTRACK*/
- (*current_max)--;
- }
- solve_DGR(next_x, next_y, board, board_size, current_max, overall_max);
- return;
- }
- int main(int argc, char** argv) {
- int board_size;
- int board[MAX][MAX];
- int current_max;
- int overall_max;
- scanf("%d", &board_size);
- while (board_size >0){
- current_max = 0;
- overall_max = 0;
- reset_board(board);
- fill_board(board, board_size);
- solve_DGR(0,0, board, board_size, ¤t_max, &overall_max);
- printf("%d\n", overall_max);
- scanf("%d", &board_size);
- }
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement