2013/03/11

XOX /SPOJ-CSMS/


Амихандаа хиймэл оюун ухаантай байхаар л бодсон юм шүү дээ. Хиймэл оюун ухаан гэх ч юм уу даа. Хоёр тоглогч хамгийн сайнаараа нүүдэг байхаар л бодож хийсэн юм.


http://www.spoj.com/CSMS/problems/CSMS0006/

#include <stdio.h>

char board[3][4];
struct NUD
{
 int h;
 int x;
 int y;
}nud[4];
int f(char p, int n);
void print()
{
 int i, j;
 for(i = 0; i < 3; i++)
 {
  for(j = 0; j < 3; j++)
   putchar(board[i][j]);
  putchar('\n');
 }
 putchar('\n');
}
int main()
{
 int i;
 int j;
 int k = 0;
 int winner;
 for(i = 0; i < 3; i++)
 for(j = 0; j < 4; j++)
 {
  board[i][j] = getchar();
  if(board[i][j] == '#')
  {
   nud[k].h = 1;
   nud[k].x = i;
   nud[k].y = j;
   k++;
  }
 }
 winner = f('X', 3);
 switch(winner)
 {
 case 2: printf("X"); break;
 case 1: printf("#"); break;
 case 0: printf("O"); break;
 }
 return 0;
}

int f(char p, int n)
{
 int i;
 int minPoint = 2, point;
 //print();
 for(i = 0; i < 3; i++)
 {
  if(nud[i].h == 0)
   continue;
  board[nud[i].x][nud[i].y] = p;
  //print();
  if((board[0][nud[i].y] == p && board[1][nud[i].y] == p && board[2][nud[i].y] == p) ||
     (board[nud[i].x][0] == p && board[nud[i].x][1] == p && board[nud[i].x][2] == p) ||
     (board[0][0] == p && board[1][1] == p && board[2][2] == p) ||
     (board[0][2] == p && board[1][1] == p && board[2][0] == p))
   return 2;
  board[nud[i].x][nud[i].y] = '#';
 }
 if(n == 1) return 1;
 for(i = 0; i < 3; i++)
 {
  if(nud[i].h == 0)
   continue;
  board[nud[i].x][nud[i].y] = p;
  nud[i].h = 0;
  point = f((p == 'X')?'O':'X', n - 1);
  if(point < minPoint)
   minPoint = point;
  board[nud[i].x][nud[i].y] = '#';
  nud[i].h = 1;
 }
 return (minPoint == 2)? 0 : (minPoint == 0) ? 2 : 1;
}

No comments:

Post a Comment