1008.Gnome Tetravex
来源:互联网 发布:欧奈尔选股公式源码 编辑:程序博客网 时间:2024/06/05 15:59
原题大意:判断相邻的正方形块能否拼成三角形标号相邻的在一起。
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 <stdio.h>#include <string.h>int n ;int a[ 25 ][ 4 ] ;int state[ 25 ] ; //用来标记每一种正方形出现的次数int result[ 25 ] ;int q ;int Game ( int index ){ int i ; if ( index == n*n ) return 1 ; else { for ( i = 0 ; i < q ; i ++ ) { // 如果该位置被用完则跳过 if ( state[ i ] == 0 ) continue ; else { // 排掉最上面那一排 同时下面和上面相等 if ( index >= n && a[ i ][ 0 ] != a[ result[ index-n ] ][ 2 ] ) continue ; // 排掉最左边那一列 同时 右边和左边相等 if ( index%n != 0 && a[ result[ index - 1] ][ 1 ] != a[ i ][ 3 ] ) continue ; state[ i ] -- ; result[ index ] = i ; //如果能到这说明找到了一个合适的 if ( Game( index+1 ) ) // 递归继续找,如果找到了就返回1 return 1 ; state[ i ] ++ ; // 回溯 如果不成功再试i++ 但在试之前,要把原来的状态改动改回来 } } } return 0;}int main ( ){ int num = 0 ; memset ( a , 0 , sizeof( a ) ) ; memset ( state , 0 , sizeof( state ) ) ; memset ( result , 0 , sizeof( result ) ) ; while ( scanf("%d",&n) , n ) { num++ ; q = 0 ; int i , j , top , right , bottom , left ; for ( i = 0 ; i < n*n ; i ++ ) { scanf("%d %d %d %d",&top , &right , &bottom , &left ); // printf("0k\n"); for ( j = 0 ; j < q ; j ++ ) { /* 把相同的正方形标记 */ if ( a[ j ][ 0 ] == top && a[ j ][ 1 ] == right && a[ j ][ 2 ] == bottom && a[ j ][ 3 ] == left ) { state[ j ] ++ ; break; } } if ( j == q ) { a[ j ][ 0 ] = top ; a[ j ][ 1 ] = right ; a[ j ][ 2 ] = bottom ; a[ j ][ 3 ] = left ; state[ q ] = 1 ; q ++ ; //q为有效的正方形的个数即互不相同 } } // 每个结果之间都要有空行 if ( num > 1 ) printf("\n"); printf("Game %d: ", num ); if ( Game ( 0 ) ) printf("Possible\n"); else printf("Impossible\n"); } return 0;}
- 1008.Gnome Tetravex
- Gnome Tetravex
- Gnome Tetravex
- zoj 1008 Gnome Tetravex
- 1008 Gnome Tetravex
- ZOJ 1008 Gnome Tetravex
- ZOJ1008 Gnome Tetravex
- zju 1008 Gnome Tetravex
- zoj 1008 Gnome Tetravex
- ZOJ 1008Gnome Tetravex
- zju1008题解Gnome Tetravex
- ZOJ1008 Gnome Tetravex
- ZOJ 1008 Gnome Tetravex
- ZOj 1008 Gnome Tetravex
- 1008-Gnome Tetravex -zju
- zoj1008-gnome tetravex
- ZOJ-1008-Gnome Tetravex
- Gnome Tetravex zoj
- 如何查看linux版本信息
- 以Base64字符串形式上传文件——服务器端Java/Servlet接受请求数据中的字符串内容并转换成文件
- 1051. 复数乘法 (15)
- HTML5的基本结构
- Leetcode352——Data Stream as Disjoint Intervals
- 1008.Gnome Tetravex
- Nginx 教程二:利用nginx搭建静态文件服务、正向代理服务器、反向代理服务器
- 解决办法:CMake编译时出现“error in configuration process project files may be invalid”
- Problem 1002-2017 ACM/ICPC Asia Regional Shenyang Online
- 博客开篇
- zooKeeper无法启动问题解决
- 欢迎使用CSDN-markdown编辑器
- 欢聚时代2017校招笔试题目(web前端类)B卷
- 单链表的删除指定节点解析