Syins写的dfs例题——Gnome Tetravex加魔改版

Gnome Tetravex

Time Limit: 10000 ms
Memory Limit: 32768 KB

Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given nxn squares. Each square is divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles are the left triangle, the top triangle, the right triangle and the bottom triangle. For example, Fig. 1 shows the initial state of 2x2 squares.

Fig. 1 The initial state with 2*2 squares

The player is required to move the squares to the termination state. In the termination state, any two adjoining squares should make the adjacent triangle marked with the same number. Fig. 2 shows one of the termination states of the above example.

Fig. 2 One termination state of the above example

It seems the game is not so hard. But indeed, Hart is not accomplished in the game. He can finish the easiest game successfully. When facing with a more complex game, he can find no way out.

One day, when Hart was playing a very complex game, he cried out, “The computer is making a goose of me. It’s impossible to solve it.” To such a poor player, the best way to help him is to tell him whether the game could be solved. If he is told the game is unsolvable, he needn’t waste so much time on it.

Input

The input file consists of several game cases. The first line of each game case contains one integer n, 0 <= n <= 5, indicating the size of the game.

The following n*n lines describe the marking number of these triangles. Each line consists of four integers, which in order represent the top triangle, the right triangle, the bottom triangle and the left triangle of one square.

After the last game case, the integer 0 indicates the termination of the input data set.

Output

You should make the decision whether the game case could be solved. For each game case, print the game number, a colon, and a white space, then display your judgment. If the game is solvable, print the string “Possible”. Otherwise, please print “Impossible” to indicate that there’s no way to solve the problem.

Print a blank line between each game case.

Note: Any unwanted blank lines or white spaces are unacceptable.

Sample Input

2
5 9 1 4
4 4 5 6
6 8 5 4
0 4 4 3
2
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
0

Output for the Sample Input

Game 1: Possible

Game 2: Impossible



这道题目明显就是dfs,而且条件简单,很容易就能写出来,很难wa,不过它10s应该会给很多人信心,但是直接简单的25判,不出意外的话应该是TLE
TLE的话就要考虑优化了,那么这道题的优化是什么呢?
有没有想过可能会出现一样的棋子,如果5x5出现了哪怕1个重复棋子,直接就少了一大堆可能,因为重复的话可以通判(但是实际上它没有降低上限,但是能过,应该是本来也没想让人解决上限问题,应该是给了有很多重复的5x5,或者可以直接一遍得到结论,写个变量快速返回到主函数就ok)
于是

代码👇

#include<iostream>
using namespace std;
struct xxx
{
    int s,x,z,y,o;//这里用结构体存的上下左右,方便看。o是该类型的未摆放个数
}a[25],b[25];
int n,f,c;
void dfs(int d);
int main()
{
    int x=0,zi,si,yi,xi;
    while(cin>>n){
        f=c=0;
        if(n==0) break;
        if(x)cout<<endl;
        for(int i=0;i<n*n;i++){
            cin>>zi>>si>>yi>>xi;
            int k=1;
            for(int j=0;j<c;j++){
                if(a[j].z==zi&&a[j].s==si&&a[j].y==yi&&a[j].x==xi){
                    k=0;
                    a[j].o++;
                    break;
                }
            }
            if(k){
                a[c].z=zi;
                a[c].s=si;
                a[c].y=yi;
                a[c].x=xi;
                a[c++].o=1;
            }
        }
        dfs(0);
        cout<<"Game "<<++x<<':';
        if(f)cout<<" Possible"<<endl;
        else cout<<" Impossible"<<endl;
    }
    return 0;
}
void dfs(int d)
{
    if(f)return;
    if(d==n*n){
        f=1;
        return;
    }
    for(int i=0;i<c;i++){
        if(a[i].o){
            if(d==0){
                b[d]=a[i];
                a[i].o--;
                dfs(d+1);
                a[i].o++;
            }
            else if(d<n){
                if(a[i].z==b[d-1].y){
                    b[d]=a[i];
                    a[i].o--;
                    dfs(d+1);
                    a[i].o++;
                }
            }
            else if(d%n==0){
                if(a[i].s==b[d-n].x){
                    b[d]=a[i];
                    a[i].o--;
                    dfs(d+1);
                    a[i].o++;
                }
            }
            else{
                if(a[i].s==b[d-n].x&&a[i].z==b[d-1].y){
                    b[d]=a[i];
                    a[i].o--;
                    dfs(d+1);
                    a[i].o++;
                }
            }
        }
    }
}

输出的结构对我来说还是有点麻烦的,看了好几遍题才发现还要输出一个空行,之前还傻傻的加到Game这一行输出后面,直接pe(一共4次/(ㄒoㄒ)/)后来想到0的话确实前面不应该有空行,又改才过的,/(ㄒoㄒ)/

再分享一下另外一个版本
我又想了个题目给自己写其实是在写这个题目的时候一开始英语差看不明白,自己脑补了题目
先有一个数字n
接下来有2n行数据,每行有2n个数据,每个奇数行与接下来的偶数行共同构成n组数据,em。。。
总而言之是这样的:
在这里插入图片描述
然后每组从第一个按照顺时针顺序分别作为上,右,下,左;
然后,每个块可以旋转
输出能不能达到跟上面那题同样的条件
接下来就跟上面的一样了

哭着贴出代码👇

#include<iostream>
using namespace std;
int n,f;
struct xxx
{
    int s,x,z,y;
}a[25];
void dfs(int s);
int main()
{
    int x=0,k;
    while(cin>>n){
        if(n==0)break;
        k=0;
        f=0;
        for(int i=0;i<n;i++){
           for(int j=0;j<n;j++){
                cin>>a[i*n+j].s>>a[i*n+j].y;
           }
           for(int j=0;j<n;j++){
                cin>>a[i*n+j].z>>a[i*n+j].x;
           }
        }
        cout<<"Game "<<++x<<':';
        dfs(0);
        if(f)cout<<"Possible"<<endl;
        else cout<<"Impossible"<<endl;
    }
    return 0;
}
void dfs(int s)
{
    int i,t;
    if(f)return;
    if(s==n*n){
        f=1;
        return;
    }
    if(s==0);
    else if(s%n==0)
        if(a[s].s!=a[s-n].x)return;
    else if(s<n)
        if(a[s].z!=a[s-1].y)return;
    else if((a[s].s!=a[s-n].x)||(a[s].z!=a[s-1].y))return;
    dfs(s+1);
    for(i=0;i<3;i++)
    {
        t=a[s].s;
        a[s].s=a[s].z;
        a[s].z=a[s].x;
        a[s].x=a[s].y;
        a[s].y=t;
        dfs(s+1);
    }
}

不过这里没考虑什么格式,因为写完后我发现连第一个实例都错,然后翻译了一下,em。。。。
重写。。。。。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值