HDU 3368 Reversi
Reversi
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1364 Accepted Submission(s): 540
Problem Description
Reversi, also called Othello, is a two-sided game.
Each of the two sides corresponds to one player; they are referred to here as light and dark after the sides of Othello pieces, but "heads" and "tails" would identify them equally as well, so long as each marker has sufficiently distinctive sides.
Originally, Reversi did not have a defined starting position. Later it adopted Othello's rules, which state that the game begins with four markers placed in a square in the middle of the grid, two facing light-up, two pieces with the dark side up. The dark player makes the first move.
![](http://acm.hdu.edu.cn/data/images/C288-1008-1.jpg)
Dark must place a piece with the dark side up on the board, in such a position that there exists at least one straight (horizontal, vertical, or diagonal) occupied line between the new piece and another dark piece, with one or more contiguous light pieces between them. In the below situation, dark has the following options indicated by transparent pieces:
![](http://acm.hdu.edu.cn/data/images/C288-1008-2.jpg)
After placing the piece, dark turns over (flips, captures) all light pieces lying on a straight line between the new piece and any anchoring dark pieces. All reversed pieces now show the dark side, and dark can use them in later moves—unless light has reversed them back in the meantime. In other words, a valid move is one where at least one piece is reversed.
If dark decided to put a piece in the topmost location (all choices are strategically equivalent at this time), one piece gets turned over, so that the board appears thus:
![](http://acm.hdu.edu.cn/data/images/C288-1008-3.jpg)
Now light plays. This player operates under the same rules, with the roles reversed: light lays down a light piece, causing a dark piece to flip. Possibilities at this time appear thus (indicated by transparent pieces):
![](http://acm.hdu.edu.cn/data/images/C288-1008-4.jpg)
Light takes the bottom left option and reverses one piece:
![](http://acm.hdu.edu.cn/data/images/C288-1008-5.jpg)
Players take alternate turns. If one player cannot make a valid move, play passes back to the other player. When neither player can move, the game ends. This occurs when the grid has filled up, or when one player has no more pieces on the board, or when neither player can legally place a piece in any of the remaining squares. The player with the most pieces on the board at the end of the game wins.
Now after several rounds, it’s dark’s turn. Can you figure out the largest number of light pieces he can turn over?
Each of the two sides corresponds to one player; they are referred to here as light and dark after the sides of Othello pieces, but "heads" and "tails" would identify them equally as well, so long as each marker has sufficiently distinctive sides.
Originally, Reversi did not have a defined starting position. Later it adopted Othello's rules, which state that the game begins with four markers placed in a square in the middle of the grid, two facing light-up, two pieces with the dark side up. The dark player makes the first move.
![](http://acm.hdu.edu.cn/data/images/C288-1008-1.jpg)
Dark must place a piece with the dark side up on the board, in such a position that there exists at least one straight (horizontal, vertical, or diagonal) occupied line between the new piece and another dark piece, with one or more contiguous light pieces between them. In the below situation, dark has the following options indicated by transparent pieces:
![](http://acm.hdu.edu.cn/data/images/C288-1008-2.jpg)
After placing the piece, dark turns over (flips, captures) all light pieces lying on a straight line between the new piece and any anchoring dark pieces. All reversed pieces now show the dark side, and dark can use them in later moves—unless light has reversed them back in the meantime. In other words, a valid move is one where at least one piece is reversed.
If dark decided to put a piece in the topmost location (all choices are strategically equivalent at this time), one piece gets turned over, so that the board appears thus:
![](http://acm.hdu.edu.cn/data/images/C288-1008-3.jpg)
Now light plays. This player operates under the same rules, with the roles reversed: light lays down a light piece, causing a dark piece to flip. Possibilities at this time appear thus (indicated by transparent pieces):
![](http://acm.hdu.edu.cn/data/images/C288-1008-4.jpg)
Light takes the bottom left option and reverses one piece:
![](http://acm.hdu.edu.cn/data/images/C288-1008-5.jpg)
Players take alternate turns. If one player cannot make a valid move, play passes back to the other player. When neither player can move, the game ends. This occurs when the grid has filled up, or when one player has no more pieces on the board, or when neither player can legally place a piece in any of the remaining squares. The player with the most pieces on the board at the end of the game wins.
Now after several rounds, it’s dark’s turn. Can you figure out the largest number of light pieces he can turn over?
Input
The first line contains one integer T representing the number of test cases.
For each test case, there’re 8 lines. Each line contains 8 characters (D represents dark, L represents light, * represents nothing here).
Every two adjacent cases are separated by a blank line.
For each test case, there’re 8 lines. Each line contains 8 characters (D represents dark, L represents light, * represents nothing here).
Every two adjacent cases are separated by a blank line.
Output
For
each test case, in one line print the case number and the largest
number of light pieces the dark player can turn over. If he can’t put
one piece in any position, then print 0.
Please follow the format of the sample output.
Please follow the format of the sample output.
Sample Input
3
********
********
********
***LD***
***DL***
********
********
********
********
********
**DLL***
**DLLL**
**DLD***
********
********
********
********
********
*D******
*DLLD***
***LL***
**D*D***
********
********
Sample Output
Case 1: 1
Case 2: 3
Case 3: 0
Source
Recommend
开始做错的原因就是因为没考虑这种情况,
*****D**
*****D**
*****L**
*****L**
*****L**
DLLLL***
********
********
********
后来稍微修改了一下就a了
1 #include<math.h> 2 #include<stdio.h> 3 #include<string.h> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define N 12 8 9 int dx[]={1,-1, 0, 0,1, 1,-1,-1}; 10 int dy[]={0, 0, 1,-1,1,-1,-1, 1}; 11 12 13 int n; 14 char mat[N][N]; 15 int v[N][N]; 16 int dfs1(int x,int y,int t) 17 { 18 if(mat[x][y]=='D')return t; 19 if(mat[x][y]=='*')return 0; 20 // printf("dfs(%d,%d,%d)\n",x,y,t+1); 21 dfs1(x+1,y,t+1); 22 } 23 int dfs2(int x,int y,int t) 24 { 25 if(mat[x][y]=='D')return t; 26 if(mat[x][y]=='*')return 0; 27 // printf("dfs2(%d,%d,%d)\n",x,y,t+1); 28 dfs2(x-1,y,t+1); 29 } 30 int dfs3(int x,int y,int t) 31 { 32 if(mat[x][y]=='D')return t; 33 if(mat[x][y]=='*')return 0; 34 // printf("dfs3(%d,%d,%d)\n",x,y,t+1); 35 dfs3(x,y+1,t+1); 36 } 37 int dfs4(int x,int y,int t) 38 { 39 if(mat[x][y]=='D')return t; 40 if(mat[x][y]=='*')return 0; 41 // printf("dfs4(%d,%d,%d)\n",x,y,t+1); 42 dfs4(x,y-1,t+1); 43 } 44 int dfs5(int x,int y,int t) 45 { 46 if(mat[x][y]=='D')return t; 47 if(mat[x][y]=='*')return 0; 48 // printf("dfs4(%d,%d,%d)\n",x,y,t+1); 49 dfs5(x+1,y+1,t+1); 50 } 51 int dfs6(int x,int y,int t) 52 { 53 if(mat[x][y]=='D')return t; 54 if(mat[x][y]=='*')return 0; 55 // printf("dfs4(%d,%d,%d)\n",x,y,t+1); 56 dfs6(x-1,y-1,t+1); 57 } 58 int dfs7(int x,int y,int t) 59 { 60 if(mat[x][y]=='D')return t; 61 if(mat[x][y]=='*')return 0; 62 // printf("dfs4(%d,%d,%d)\n",x,y,t+1); 63 dfs7(x-1,y+1,t+1); 64 } 65 int dfs8(int x,int y,int t) 66 { 67 if(mat[x][y]=='D')return t; 68 if(mat[x][y]=='*')return 0; 69 // printf("dfs4(%d,%d,%d)\n",x,y,t+1); 70 dfs8(x+1,y-1,t+1); 71 } 72 int dfs(int x,int y,int t) 73 { 74 int mm=0; 75 76 mm+=dfs1(x+1,y,t); 77 mm+=dfs2(x-1,y,t); 78 mm+=dfs3(x,y+1,t); 79 mm+=dfs4(x,y-1,t); 80 81 mm+=dfs5(x+1,y+1,t); 82 mm+=dfs6(x-1,y-1,t); 83 mm+=dfs7(x-1,y+1,t); 84 mm+=dfs8(x+1,y-1,t); 85 86 return mm; 87 } 88 89 90 int main() 91 { 92 int t;cin>>t;getchar(); 93 int tt=1; 94 for(int i=0;i<=9;i++) 95 { 96 mat[0][i]='*'; 97 mat[9][i]='*'; 98 } 99 for(int i=0;i<=9;i++) 100 { 101 mat[i][0]='*'; 102 mat[i][9]='*'; 103 } 104 105 while(t--) 106 { 107 memset(v,0,sizeof(v)); 108 int ma=0; 109 for(int i=1;i<=8;i++) 110 for(int j=1;j<=8;j++) 111 { 112 scanf(" %c",&mat[i][j]); 113 } 114 115 for(int i=1;i<=8;i++) 116 { 117 for(int j=1;j<=8;j++) 118 { 119 if(mat[i][j]=='*') 120 { 121 // printf("%d,%d\n",i,j); 122 ma=max(ma, dfs(i,j,0) ); 123 // printf("%d,%d ma = %d\n",i,j,ma); 124 } 125 } 126 } 127 printf("Case %d: ",tt++); 128 cout<<ma<<endl; 129 } 130 return 0; 131 } 132 133 /* 134 33 135 ******** 136 ******** 137 ******** 138 ***LD*** 139 ***DL*** 140 ******** 141 ******** 142 ******** 143 144 ******** 145 ******** 146 **DLL*** 147 **DLLL** 148 **DLD*** 149 ******** 150 ******** 151 ******** 152 153 ******** 154 ******** 155 *D****** 156 *DLLD*** 157 ***LL*** 158 **D*D*** 159 ******** 160 ******** 161 162 ******** 163 ******** 164 *D****** 165 *DLLD*** 166 ***LL*** 167 **D*L*** 168 *****L** 169 ******** 170 171 ******D* 172 *****L** 173 *D**L*** 174 *DLLLLL* 175 **LLL*** 176 *LD*L*** 177 *****L** 178 ******** 179 180 181 182 */