zju1008题解Gnome Tetravex

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


题目大意:

在N*N的方格中,每一个方格有四个三角形按照上右下左的顺序次序排列,然后移动方格,是那些相邻的方格,满足相邻的对应三角形的值相等,有时候是无解的,有时候有解,题目就是让写下程序来判断给定一个方格看看是否能满足题意,如果能就打印possibble

如果不能就打印impossible


分析题意:

显然没有其他别的思路的情况下,只能用搜索了,其实也就是枚举或者说是全排列,只不过是这里对象不是数字的按照一定的顺序排列而是图形按照一定的规则来进行排列,

我们把所有的情况都给看看,如果能拼成,那就可以,如果所有的情况都找不到,那就是不可能了。

    所以下面其实就是来想想用全排列,把每一个方格所出现的可能性都给试一遍,总会找到结果的。

     那么核心的算法思想就是全排列了,下面简单写下全排列的过程。

如果有N*N个方格的话,按照行的顺序把每一个方格编上序号,从0到n*N-1;


place函数就是在当前位置放块。

//开始的时候初始化所有的tag[i]为false

     int place(int pos){

if(pos==N*N)return 1;//递归的终止条件如果都能放下,那么就一次向上返回一个值1,那么最开始的值也为1.否则下面的那个循环结束后,每一个都试了一遍还找不到返回值1去结束循环的话,那么循环最终结束后就执行return 0说明没有解

for(i=0;i<n*n;i++){

if(!tag[i]&&placeble(i)){

tag[i]=true;

table[pos]=i;

if(place(pos+1)==1)return 1;

tag[i]=false;//每次递归返回后执行当前的循环的时候,都要保持原tag中的值不能改变。这相当于执行该点开始的另一次搜索,所以值不能改变,改变之后一定要换回来。

}

return 0;

}

placeable(i)是判断是否可以在当前位置放置第i个方块的判断函数

上面的方法是一般的方法,但是带来很大的为题就是当n为5时,那就是25的阶乘,数会非常的大,很耗时,所以要进行优化。

通过分析,我们发现,这些块中如果存在很多重复的块(块的种类相同)时,这样做就太浪费了,所以我们可以不用按照块的数目来计算,可以变成块的种类来计算,这样就会大大减小数据量,如果存在10个块相同时,那么就是15的阶乘,会大大减小判断的情况,其实说到底就是说,这个全排列中可能会存在那些相同的项,死板的用数目 阶乘是不合理的,我们要判断是否有相同项,如果有的话,我们要进行处理,问题则转化为,求可能含有相同项的阶乘问题。


这样的话,我们就可以首先来统计下,这些块有那些种类,然后按照种类的个数来进行判断,这样就减少了工作量。

下面就是源码以及注释

#include<stdio.h>
//数据结构
int square[25][4];//最多有25个块,每个块里有四个部分,分别用一行的数组来表示可以体现他们的顺序关系 
int q;//总的块的种类个数 
int kindcount[25];//总的种类可能有25种,这是最坏的情况,如果有相同的类型的话,那么就把对应那个值加1
//也就是说,这个数组里面保存的是每种种类的个数, 注意该数组可能用不完那么多,只用了q个,每一个
//都保存着这种种类编号的个数 


int n;
int table[25];//当前实际位置放置的种类
int place(int pos){
    if(pos==n*n)return 1;
    for(int i=0;i<q;i++){//对于每次的一个循环其实就是一次搜索,注意跟深度搜索联系,这里跟最后那个为什么要恢复也有联系,两者联系起来理解更好 
        if(kindcount[i]==0)continue;//如果该种种类的数目为零则找下一个 
        if(pos%n!=0)//如果不是最左边就水平判断 
            if(square[table[pos-1]][1]!=square[i][3])continue;
            //如果不是第一行就垂直判断 
        if(pos>=n)if(square[table[pos-n]][2]!=square[i][0])continue;
        table[pos]=i;
        kindcount[i]--;
        if(place(pos+1)==1)return 1;
        kindcount[i]++;//此处一定要恢复过来,因为这个数组就相当于一个固定保存器,它里面的值一次搜索完成后都要回复原来的样子
        //不然这个数组的值都改变了,我们该怎么判断呢?这点不容易理解,要多想想为什么。 
       }
       return 0;
    }
int main(){
    int iCase=0; 
    while(scanf("%d",&n)&&n){
        int i,j,top,right,bottom,left;
         q=0;
        iCase++;
        for(i=0;i<n*n;i++){
            scanf("%d%d%d%d",&top,&right,&bottom,&left);
            for( j=0;j<q;j++){//每次读进来四个值,就判断是否有重复,如果有就记录下 
                if(square[j][0]==top&&square[j][1]==right&&square[j][2]
                                ==bottom&&square[j][3]==left){
                                    kindcount[j]++;
                                    break;
                                    //如果有重复那么当前这个值就不必再放到块中了,因为我们是为了初始化kindcount的值 
                                    }
                }
                if(j==q){//如果没有就把值放到当前的块中 
                    square[j][0]=top;
                    square[j][1]=right;
                    square[j][2]=bottom;
                    square[j][3]=left;
                    kindcount[j]=1;
                    q++;
                    }
            }
            if(iCase>1)printf("\n");
            if(place(0)==1)printf("Game %d: Possible\n",iCase);
            else printf("Game %d: Impossible\n",iCase);
        }
        return 0;
    } 





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ZJU 型分类 ZJU_Main 主页 下一页 ZJU 型分类 文演整理版 2008-3-23 数论: 1007 Numerical Summation of a Series 简单,还是蛮有意思的 1045 HangOver 简单 1049 I Think I Need a Houseboat 简单 1028 Flip and Shift 简单,可以DP/BFS/……,但是实际上有数学方法可直接判断出来 1026 Modular multiplication of polynomials 简单,有比较简单的好算法 1307 Packets 简单,不过也蛮经典的…… 1312 Prime Cuts 简单 1334 Basically Speaking 简单 1337 Pi 简单 1342 Word Index 简单 1349 Four Quarters 简单 1350 The Drunk Jailer 简单 1352 Number Base Conversion 简单 1353 Unimodal Palindromic Decompositions 规模不大,所以是简单…… 1354 Extended Lights Out 简单 1362 Game Prediction 简单 1365 Mileage Bank 简单 1382 A Simple Task 简单 1383 Binary Numbers 简单 1403 Safecracker 简单 1408 The Fun Number System 简单 1486 Color the Tree 简单 1487 Playing Cards 简单 1489 2^x mod n = 1 简单,应该有好算法,不过枚举就可以过…… 1503 One Person "The Price is Right" 简单,POI Eggs的翻版 1512 Water Treatment Plants 简单,组合计数 1526 Big Number 简单,不过O(1)和O(n)还是有区别的:) 1529 Enigmatic Travel 简单,不过个人感觉目描述很令人费解 1530 Find The Multiple 简单 1537 Playing with a Calculator 简单 1577 GCD & LCM 简单,分区联赛的…… 1005 Jugs 简单 1543 Stripies 简单 1569 Partial Sums 简单 1062 Trees Made to Order 简单 1070 Bode Plot 简单 1073 Round and Round We Go 简单,142857,我喜欢^_^ 1078 Palindrom Numbers 简单 1086 Octal Fractions 简单 1199 Point of Intersection 简单 1104 Leaps Tall Buildings 简单 1110 Dick and Jane 简单 1115 Digital Roots 简单 1113 u Calculate e 简单 1152 A Mathematical Curiosity 简单 1154 Niven Numbers 简单 1160 Biorhythms 简单 1163 The Staircases 简单 1177 K-Magic Number 简单 1184 Counterfeit Dollar 简单 1182 Keeps Going and Going and ... 简单,我喜欢:) 1284 Perfection 简单 1272 Numerically Speaking 简单 1269 Coconuts, Revisited 简单 1247 There s Treasure Everywhere! 简单 1241 Geometry Made Simple 简单 1202 Divide and Count 简单 1216 Deck 简单 1218 Ratio 简单 1261 Prime Land 简单 1212 Mountain Landscape 无聊 1410 Number Sequence 无聊 1401 Hilbert Curve Intersections 无聊 1331 Perfect Cubes 无聊 1322 Random Number 无聊 1535 Lucky Ticket 无聊 1539 Lot 无聊 1363 Chocolate 经典…… 1366 Cash Machine 经典!强烈推荐! 1149 Dividing up 经典,可以用1366的方法做,利用问的特殊性用贪心做出来 1222 Just the Facts 经典,据说可能有O(logn)的做法,但我没想到:( 1475 Ranklist 没有完美解决,不知道您有没有好方法…… 1572 Bracelet 义不明,感觉可能是判定欧拉回路的存在性,但是过不去 1133 Smith Numbers 没有完美解决,数学 1080 Direct Subtraction 尚未解决,我过的程序是错的,想和您讨论~~~ 1229 Gift?! SRbGa的经典…… 1238 Guess the Number SRbGa的经典…… 1239 Hanoi Tower Troubles Again! SRbGa的经典…… 字符串处理: 1050 Start Up the Startup 简单 1315 Excuses, Excuses! 简单 1151 Word Reversal 简单,好像是什么Practice的…… 1170 String Matching 简单 1174 Skip Letter Code 不错的简单 1175 Word Process Machine 简单 1181 Word Amalgamation 简单 1038 T9 无聊,单词树 1330 DNA Translation 无聊 1335 Letter Sequence Analysis 无聊 1099 HTML 无聊 1243 URLs 无聊 1540 Censored! 经典!强烈推荐! 1511 Word Puzzles 没有完美解决的, 动态规划: 1011 NTA 简单 1013 Great Equipment 简单 1024 Calendar Game 简单 1027 Human Gene Functions 简单 1037 Gridland 简单 1052 Algernon s Noxious Emissions 简单 1409 Communication System 简单,但是很容易看错~~~ 1425 Crossed Matchings 简单 1438 Asteroids! 简单 1459 String Distance and Transform Process 简单 1462 Team Them Up! 简单 1556 Heroes Of Might And Magic 简单,不过背景蛮有意思的…… 1520 Duty Free Shop 简单 1524 Supermarket 简单 1301 The New Villa 简单 1303 Jury Compromise 其实不是很难,但是很容易错,555…… 1345 Best Deal 简单,但是也很容易错……555…… 1360 Radar Installation 简单 1396 The Umbrella Problem: 2054 简单 1058 Currency Exchange 简单 1076 Gene Assembly 简单 1092 Arbitrage 简单 1093 Monkey and Banana 简单 1094 Matrix Chain Multiplication 简单 1536 Labyrinth 简单 1100 Mondriaan s Dream 简单,DP可以过,不过据说有复杂的组合公式 1103 Hike on a Graph 简单 1134 Strategic Game 简单 1147 Formatting Text 简单 1148 The Game 简单 1161 Gone Fishing 简单 1180 Self Numbers 简单 1192 It s not a Bug, It s a Feature! 简单 1196 Fast Food 简单 1107 FatMouse and Cheese 简单,不过目描述有些混乱 1136 Multiple 简单,BFS 1276 Optimal Array Multiplication Sequence 简单 1255 The Path 简单 1250 Always On the Run 简单 1213 Lumber Cutting 简单 1206 Win the Bonus 简单 1479 Dweep 无聊 1587 UP 100 无聊,DP应该可以……但是太烦了……没做…… 1066 Square Ice 无聊,目前已知的O(nlogn)算法要用AVL 1245 Triangles 无聊 1022 Parallel Expectations 经典,想了n久,最后发现可以DP,相当好的一道 1499 Increasing Sequences 经典{} 1039 Number Game 感觉可以直接以所有剩下的数作为状态DP,但是缺乏证明…… 1227 Free Candies SRbGa的经典,我看了oibh上的解报告才做出来的……:( 1234 Chopsticks SRbGa的经典…… 图论: 1525 Air Raid 简单,最小路径覆盖 1500 Pre-Post-erous! 简单 1501 Knockout Tournament 简单 1508 Intervals 对您来说应该是简单,但我想了n久……:(,差分限制系统 1333 Galactic Import 简单 1304 Tin Cutter 简单,但是似乎有空间复杂度为O(n)的算法 1310 Robot 简单 1311 Network 简单 1344 A Mazing Problem 简单 1395 Door Man 简单,欧拉回路 1372 Networking 简单 1406 Jungle Roads 简单 1053 FDNY to the Rescue! 简单 1055 Oh, Those Achin Feet 不错的简单 1059 What s In a Name 简单 1064 Roads Scholar 简单 1082 Stockbroker Grapevine 简单 1085 Alien Security 简单,我觉得我当时的算法好巧妙:) 1097 Code the Tree 简单 1060 Sorting It All Out 简单,但是规模要是大些的话…… 1105 FatMouse s Tour 简单 1119 SPF 简单 1127 Roman Forts 简单 1140 Courses 简单 1157 A Plug for UNIX 蛮不错的简单 1203 Swordfish 简单 1221 Risk 简单,最短路 1197 Sorting Slides 简单,匹配 1268 Is It A Tree? 不错的,图论 1273 It s Ir-Resist-Able! 简单,图论 1298 Domino Effect 简单,最长路 1260 King 简单,差分限制系统…… 1291 MPI Maelstrom 不错的,最长路 1266 Gossiping 简单 1285 Shipping Routes 无聊,最短路 1313 Gears on a Board 无聊 1502 Plugged In 无聊,匹配应该可以,但是太烦了,没做…… 1568 WishingBone s Room Plan 无聊,最大最小匹配,不过容易看错~~~~~~ 1077 Genetic Combinations 无聊,匹配 1364 Machine Schedule 背诵……匹配…… 1137 Girls and Boys 背诵,匹配…… 1023 University Entrace Examination 经典,但是和1576重复 1576 Marriage is Stable 经典,感人的背景,经典的算法…… 1249 Pushing Boxes 经典,某人论文写过,求割点和块+BFS可以在O(面积)的时间内做出 1141 Closest Common Ancestors 没有完美解决,最简单的算法就能过,但要是规模大了…… 1084 Channel Allocation 尚未完美解决,我用搜索过的,不过是不是有更好的算法呢……? 1231 Mysterious Mountain SRbGa的经典…… 1232 Adventure of Super Mario SRbGa的经典…… 搜索: 1002 Fire Net 简单,CEOI2002的Guard的缩小版,搜索就可以 1003 Crashing Balloon 简单,很faint的目,最后发现最简单的搜索就能过 1004 Anagrams by Stack 简单,同样faint 1320 DEL Command 简单 1496 Best Fit 简单 1179 Finding Rectangles 简单 1190 Optimal Programs 简单 1217 Eight 简单 1204 Additive equations 简单 1219 Pizza Anyone? 简单 1036 Enigma 2 无聊,终于耐心读完目后发现是简单的搜索+模拟 1089 Lotto 无聊 1054 For the Porsche 无聊 1302 Ships …… 1031 Square Destroyer 变态,:(:(:(:(,狂剪枝才过的…… 1297 Hexagon 变态,标准解法是搜索…… 1008 Gnome Tetravex 经典,个人感觉是很不错的卡时搜索 1380 Microfiches 经典!强烈推荐! 1252 Island of Logic 经典,不过也够老了…… 1237 Fans and Gems SRbGa的经典,变态,但是很有趣:) 模拟: 1051 A New Growth Industry 简单 1300 Border 简单 1326 M*A*S*H 简单 1494 Climbing Worm 简单 1072 Microprocessor Simulation 简单 1098 Simple Computers 简单 1056 The Worm Turns 简单 1195 Blowing Fuses 简单 1189 Numbers That Count 简单 1144 Robbery 简单 1153 Tournament Seeding 简单,但是直接计算好像不行,得模拟……:( 1167 Trees on the Level 简单 1200 Mining 简单 1278 Pseudo-Random Numbers 简单 1257 Parking Lot 简单 1270 Nonstop Travel 简单 1207 The Knight, the Princess, and the Dragons 无聊 1169 Square Cipher 无聊 1176 Die and Chessboard 无聊 1178 Booklet Printing 无聊 1009 Enigma 无聊,但是很容易错……:( 1012 Mainframe 无聊,但是很容易错……:( 1324 Unix ls 无聊,输出格式没说清楚~~~~~~~ 1336 Mark-up 无聊 1277 Transferable Voting 无聊 1279 Cowculations 无聊 1281 Hi-Q 无聊 1282 Call Forwarding 无聊 1065 Robots 变态,太复杂啦~~~~~~~~~~~ 1208 Roll the Die! 变态 1388 Exchanges 经典!强烈推荐! 1236 Eat or Not to Eat? SRbGa的经典……模拟 几何: 1575 Koch Curve 简单 1010 Area 简单 1565 Input 简单 1081 Points Within 简单 1165 Laser Lines 简单,但是算法能否再改进呢……? 1248 Video Surveillance 简单,李彭煦的论文中写到过,好像是某年CTSC的…… 1299 Pendulum 简单,我蛮喜欢的:) 1090 The Circumference of the Circle 无聊 1271 Doing Windows 无聊 1280 Intersecting Lines 无聊 1296 Stars 经典变态…… 1030 Farmland 变态……现在我都为自己当时居然能做出来感到不可思议…… 1041 Transmitters 变态……好烦人……现在绝对没有信心再做一遍了…… 1158 Treasure Hunt 经典,zig教会我的…… 1139 Rectangles 没有完美解决 其它: 1006 Do the Untwist 简单 1014 Operand 简单 1016 Parencodings 简单 1042 W s Cipher 简单 1047 Image Perimeters 简单 1514 Fake Tickets 简单 1029 Moving Tables 简单,好像是线段树的经典目之一,但是这规模比较小,所以不必用。 1504 Slots of Fun 简单 1314 Uniform Generator 简单 1325 Palindromes 简单 1319 Black Box 简单,但当初我没做出来……被zig教会的……555…… 1341 LED Test 简单 1338 Up and Down Sequences 简单 1339 Machined Surfaces 简单 1358 Moving Object Recognition 简单 1392 The Hardest Problem Ever 简单 1402 Magnificent Meatballs 简单 1405 Tanning Salon 简单 1414 Number Steps 简单 1423 (Your)((Term)((Project))) 简单 1427 An Old Stone Game 简单 1439 Area Ratio 简单 1449 Maximum Sum 简单 1476 Weird Clock 简单 1477 File Compression 简单,不过我一直没做出来……后来被LC教会的……@&*:)…… 1481 Namenum 简单 1482 Partitions 简单,不过限制了空间…… 1484 Minimum Inversion Number 简单 1493 DPCM Encoding 简单 1495 Stacking Tower 简单 1057 Undercut 简单 1518 This Sentence is False 简单,并查集 1538 Cipher 简单 1546 Fillword 简单,实在是有点…… 1552 Decoding Task 简单 1061 Web Navigation 简单 1063 Space Station Shielding 简单 1067 Color Me Less 简单 1068 P,MTHBGWB 简单 1071 Follow My Logic 简单,但很可爱:) 1074 To the Max 简单 1075 Set Me 简单 1079 Robotic Jigsaw 简单,和我推荐的那不同,这个说了可以根据条件确定拼法 1083 Frame Stacking 简单 1088 System Overload 简单,约瑟夫环 1091 Knight Moves 简单 1095 Humble Numbers 简单 1188 DNA Sorting 简单 1171 Sorting the Photos 简单 1156 Unscrambing Images 简单 1142 Maze 简单 1128 Atlantis 简单 1120 Chairlift 简单 1122 Clock 简单 1125 Floating Point Numbers 简单 1117 Entropy 简单 1109 Language of FatMouse 简单,Dictionary的实现…… 1143 Data Bugs 简单 1146 LC-Display 简单,LC…… 1159 487-3279 简单 1164 Software CRC 简单 1168 Function Run Fun 简单 1191 The Die Is Cast 简单,并查集/bfs 1286 Slurpys 不错的简单 1292 Integer Inquiry 简单 1293 Floppies 简单 1294 Golf 简单 1295 Reverse Text 简单 1274 Getting Chorded 简单 1251 Box of Bricks 简单 1244 Definite Values 简单 1242 Carbon Dating 简单 1240 IBM Minus One 简单 1201 Inversion 简单 1205 Martian Addition 简单 1210 Reciprocals 简单 1246 Instant Complexity 简单,好像是某年CTSC的…… 1259 Rails 简单,构造 1033 Ambiguous Dates 无聊 1046 Double Vision 无聊,直接枚举就可以了,但是有一些很faint的极端情况 1485 Messy Matrix 无聊,河北老前辈出的,麻烦的构造~~~~~~ 1111 Poker Hands 无聊 1118 N-Credible Mazes 无聊,义实在费解…… 1126 Bio-Informatics 无聊 1150 S-Trees 无聊 1209 April Fool s Joke 无聊 1267 Mapping the Route 无聊 1263 Time 无聊 1275 Run, Run, Runaround Numbers 无聊 1283 Making the Grade 无聊 1440 Bone Sort 背诵,排序+最长不xx子序列,而且很容易看错~~~~~~~ 1025 Wooden Sticks 背诵,排序+最长不xx子序列…… 1108 FatMouse s Speed 背诵,排序+最长不xx子序列…… 1256 What Day Is It? 变态,原来现行历法这么复杂……faint…… 1265 Append 经典 1384 Alice and Bob 经典 1390 Gates 经典!强烈推荐! 1087 Cracking the Code 经典,不过要是能做成Output Only就更好了…… 1069 Plato s Blocks 经典,没有解决得非常完美…… 1102 Phylogenetic Trees Inherited 经典,贪心 1130 Ouroboros Snake 经典,构造 1101 Gamblers 尚未完美解决,我开了巨大的hash才过的:( 1498 Galactic Breakup 尚未完美解决……有没有“拆查集”? 1019 Illusive Chase 没给规模,感觉有比较好的算法,最后弱智算法居然0.01秒通过
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值