P7395 Pinball Game (2021 CoE-I C)

Description

description

Alice is a little tired of the pinball game, she often plays this game on the computer. The reason why she is bored is because she is already an expert in this game, and she can always tie the computer.
Alice created a new computer game. The following are the rules of this two-person computer game:

(1) The game is played on a diamond-shaped board as shown in the figure below;
Insert picture description here
(2) Two players take turns to place marbles, which can be placed continuously in the horizontal, vertical, 4545-degree diagonal, and 135-135-degree diagonal directions where no marbles are placed. 11 to 33 marbles. If the player can place marbles, he must place at least 11 marbles. The following is an example of a legal single placement operation (black dots indicate that marbles have been placed, and white dots indicate that no marbles have been placed. The board is empty before this operation): The
Insert picture description here
following are examples of illegal single placement operations ( The black dot indicates that marbles are placed, and the white dot indicates that marbles are not placed. The board is empty before this operation):
Insert picture description here
Image source: proxy game http://www.walajiao.com/agent game

Explanation of the illegal reason: (aa) the three marbles are not on the same diagonal line (or vertical line); (bb) there is a space between the two marbles; (cc) the three marbles are not on the same diagonal line ; (Dd) Three marbles are not on the same diagonal line (or vertical line); (ee) 44 marbles are placed at one time; (ff) Three marbles are not on the same horizontal line (or vertical line or diagonal) on-line.

(3) If a player can no longer place marbles, that player loses the game and another player wins.

Alice is always the first to play the game, and often plays this game with Bob. Bob may leave after performing several game operations and hand over the game to the computer agent. The computer always places marbles according to the optimal strategy. Given the state of the game after Bob leaves, your task is to determine whether Alice is likely to win against the computer.

Input format The
input contains multiple sets of test data.

The first line of input contains an integer TT, which represents the number of groups of test data. Then there is a blank line. Next is the TT group of data representing the state of the board. Each group of data is composed of seven lines of characters, indicating the state of the game after Bob leaves. * indicates that marbles have been placed at this position, and. Indicates that no marbles have been placed at this position. There is a blank line between two adjacent sets of test data.

Output format
For each set of test data, if \operatorname{Alice}Alice can win, output Possible., otherwise output Impossible..

Sample input and output

Enter #1

6

   *
  * *
 * * *
* * * *
 . * *
  . *
   .

   *
  * *
 * * *
. . . *
 * * *
  * *
   *

   *
  * *
 * * .
* * * *
 * * .
  * *
   *

   *
  * *
 . . *
* * * *
 * * *
  * *
   *

   .
  * *
 * * *
* * * .
 * * *
  * *
   *

   .
  * .
 * * .
* * * .
 * * *
  * *
   *

Output #1

Possible.
Possible.
Possible.
Possible.
Impossible.
Possible.

Instructions/tips

Example description For the
first set of data, Alice can choose to place 33 marbles in the remaining 33 free positions in the diagonal direction of the lower left corner of the board, so that subsequent computers can no longer place marbles, so Alice can win.

In the second set of data, Alice can choose to place 33 marbles continuously at one time along the remaining 33 empty positions in the fourth row, so that subsequent computers can no longer place marbles, so Alice can win.

In the third set of data, there are two consecutive free positions in the penultimate column of the chessboard. Alice can place 22 marbles at a time, making subsequent computers unable to place marbles, so Alice will win.

The fourth set of data is similar to the second set of test data. There are two consecutive free positions in the third row of the chessboard, so Alice will win.

In the fifth set of data, there are only two non-contiguous free positions left on the chessboard. Since Alice can only choose one free position to place 11 marbles at a time, no matter how Alice operates, the computer can always play the remaining chessboard at one time. The beads fill up so that Alice can no longer place marbles, so Alice will lose the game.

For the sixth set of data, Alice can choose to place 22 marbles in the middle two free positions in the diagonal direction of the upper right corner of the chessboard, so that the state of the chessboard is transformed into the fifth set of data entered by the sample, so Alice will win the game.

Start of feature film

  • The game is played on a 4×4 diamond-shaped chessboard;
  • Two players take turns to place marbles. You can place 1 to 3
    marbles continuously in the horizontal, vertical, 45-degree diagonal, and 135-degree diagonal directions where no marbles are placed . Players must place marbles if they can place marbles. Place at least 1 marble.
  • If a player can no longer place marbles, that player loses the game and the other player wins.

Solution

Although it is a routine, it is after all a routine that has not been done before. Write a question to explain it.

First of all, we can directly consider the state pressure, the board number is shown in the figure:
Insert picture description here
Picture source: agent game http://www.walajiao.com/agent game

Then you make a table to show all the possible moves (state pressure). For example, if I want to put a chess piece on top of 1−5−9, it is (100010001)2.

Because it is a list of 82 situations that is hand-printed in the form of C++ output, the generator is not attached.

Then you play a DP, and set fS as the current board state S (the i-th of S is 1 means that this grid is occupied, and vice versa) whether the first player must win or the first player must lose or do not know (respectively corresponding to the number 1/0/ −1).

The initial state is ∀i∈[0,2n−1), fi=−1; f2n−1=0.

Then you remember to search to find out all the statuses.

Then just answer the question. It’s just not clear why so many characters are read into the card IO. It is obvious that not many groups are the same.

#include<bits/stdc++.h>
using namespace std;
int t,n=7,m[8]={
    
    1,2,3,4,3,2,1},id,f[(1<<16)+10];
char s[10];
const int upper=(1<<16);
const int ID[10][10]={
    
    {
    
    0},{
    
    4,1},{
    
    8,5,2},{
    
    12,9,6,3},{
    
    13,10,7},{
    
    14,11},{
    
    15}};
const int walking[90]={
    
    1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,17,3,18,272,48,34,6,288,36,4352,768,544,96,68,12,4608,576,72,12288,8704,1536,1088,192,9216,1152,24576,17408,3072,2176,18432,49152,34816,33,528,66,8448,1056,132,16896,2112,33792,136,273,7,1057,4368,16912,112,546,2114,14,292,1792,8736,224,33824,1092,4672,584,28672,3584,17472,2184,9344,57344,34944};
inline int unionset(int x,int y){
    
    return x|y;}
inline int intersection(int x,int y){
    
    return x&y;}
inline bool emptyset(int x){
    
    return x==0;}
void dfs(int board)
{
    
    
	if(~f[board])	return;
	for(int i=0;i<82;++i)
	{
    
    
		if(emptyset(intersection(board,walking[i])))
		{
    
    
			int newset=unionset(board,walking[i]);
			dfs(newset);
			if(f[newset]==0)
			{
    
    
				f[board]=1;
				return;
			}
		}
	}
	f[board]=0;
}
inline char fgc()
{
    
    
	static char buf[1<<17],*p=buf,*q=buf;
	return p==q&&(q=buf+fread(p=buf,1,1<<17,stdin),p==q)?EOF:*p++;
}
inline char fgop()
{
    
    
	char res=0;
	while((res^'*')&&(res^'.'))	res=fgc();
	return res;
}
inline void read(int &x)
{
    
    
	x=0;
	char c=fgc();
	while(isdigit(c)==0)	c=fgc();
	while(isdigit(c))	x=(x<<3)+(x<<1)+(c^'0'),c=fgc();
}
int main()
{
    
    
	read(t);
	memset(f,-1,sizeof(f));
	f[upper-1]=0;
	for(int i=0;i^upper;++i)
	{
    
    
		if(f[i]==-1)	dfs(i);
	}
	while(t--)
	{
    
    
		int board=0;
		for(int i=0;i<n;++i)
		{
    
    
			for(int j=0;j<m[i];++j)	board+=(fgop()=='*')?(1<<ID[i][j]):0;
		}
		printf(f[board]?"Possible.":"Impossible.");
		printf("\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52308504/article/details/114096710