zoukankan      html  css  js  c++  java
  • ZOJ 1008 Gnome Tetravex(DFS)

    Gnome Tetravex

    Time Limit: 10 Seconds      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 n*n 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 2*2 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

    就是把相同的块放一起,然后就没什么了、感觉意思不大
    只能说羡慕那些几十几百毫秒的代码
    #include <iostream> #include <stdio.h> #include <queue> #include <stack> #include <set> #include <vector> #include <string.h> #include <algorithm> using namespace std; struct node { int u,r,d,l; }; int map[6][6]; int n,m; int num[25]; node g[25]; int cnt; bool f; void dfs(int p) { // printf("%d ",p); if(f) return; if(p==m){f=1;return;} int i,x,y; for(i=0;i<cnt;i++) { if(num[i]==0) continue; x=p/n; y=p%n; if(x>0&&g[map[x-1][y]].d!=g[i].u) continue; if(y>0&&g[map[x][y-1]].r!=g[i].l) continue; map[x][y]=i; num[i]--; dfs(p+1); if(f) return; num[i]++; } } int main() { int i,k,t=0; int u,r,d,l; while(scanf("%d",&n),n) { m=n*n; cnt=0; // memset(map,-1,sizeof(map)); memset(num,0,sizeof(num)); for(i=0;i<m;i++) { scanf("%d %d %d %d",&u,&r,&d,&l); for(k=0;k<cnt;k++) if(g[k].u==u&&g[k].d==d&&g[k].l==l&&g[k].r==r) break; if(k==cnt) { g[k].u=u; g[k].d=d; g[k].l=l; g[k].r=r; cnt++; num[k]=1; } else num[k]++; } //printf("%d ",cnt); f=0; dfs(0); if(t) printf("\n"); printf("Game %d: ",++t); if(f) printf("Possible\n"); else printf("Impossible\n"); } return 0; }
  • 相关阅读:
    机器重装后几个要记的问题
    JS中split用法和数组中元素的删除
    免费软件的盈利方式
    web程序的发布及相关问题
    无法直接启动带有“类库输出类型”的项目
    System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
    orcale不同版本数据导入、导出及库版本查询
    VSc# web程序:gridview保存Excel文件遇到的问题
    油田生产中的几个“三”
    web跨页面传值——FORM表单(c#)
  • 原文地址:https://www.cnblogs.com/372465774y/p/2754357.html
Copyright © 2011-2022 走看看