UVa 141 The Spot Game

Code:
 1  #include<stdio.h>
 2  #include<stdlib.h>
 3  #include<iostream>
 4  #include<algorithm>
 5  #include<map>
 6  #include<string.h>
 7  using namespace std;
 8  int board[55][55];
 9  void Rotate(string &str,int n)
10  {
11      int tmp[55][55];
12      for(int i=0;i<n;i++)
13      {
14          for(int j=0;j<n;j++)
15          {
16              tmp[j][n-i-1]=board[i][j];
17          }
18      }
19      string tmpstr;
20      for(int i=0;i<n;i++)
21      {
22          for(int j=0;j<n;j++)
23          {
24              board[i][j]=tmp[i][j];
25              tmpstr+=tmp[i][j]+'0';
26          }
27      }
28      str=tmpstr;
29  }
30  int main()
31  {
32      int n;
33      while(cin>>n && n!=0)
34      {
35          memset(board,0,sizeof(board));
36          map<string,int> save;
37          int player=2,ansp,move;
38          bool flag=false;
39          for(int i=0;i<2*n;i++)
40          {
41              int a,b;
42              char ch;
43              cin>>a>>b>>ch;
44              if(flag==true) continue;
45              if(ch=='+')
46              {
47                  board[a-1][b-1]=1;
48              }
49              else if(ch=='-')
50              {
51                  board[a-1][b-1]=0;
52              }
53 
54              string str;
55              for(int j=0;j<n;j++)
56              {
57                  for(int k=0;k<n;k++)
58                  {
59                      str+=board[j][k]+'0';
60                  }
61              }
62              for(int j=0;j<4;j++)
63              {
64                  if(save[str]<i+1 && save[str]!=0)
65                  {
66                      move=i;
67                      ansp=player;
68                      flag=true;
69                      break;
70                  }
71                  else  save[str]=i+1;
72                  Rotate(str,n);
73              }
74              if(flag==true) continue;
75 
76              if(player==2) player=1;
77              else player=2;
78          }
79          if(flag==true) printf("Player %d wins on move %d\n",player,move+1);
80          else printf("Draw\n");
81      }
82      return 0;

83  }

留言

這個網誌中的熱門文章

Things a Little Bird Told Me: Confessions of the Creative Mind

UVa 12970 Alcoholic Pilots

UVa 483 Word Scramble