Taizhou OJ 3709:number Maze (array out of bounds do not report re, report wa Pit dad)

Source: Internet
Author: User

http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3709

You are playing one game called "Number Maze". The map of an example was shown in the following figure.


In the map, there is n*n+2 cells. When the game starts, you stay the top-left cells and you target are to reach the Bottom-right cell with fewest number of MO Ves. At the first step, you must move to the right of the start cell. After this, you can move to any cell (left, right, up or down, but can not move diagonally) if the target cell can divi Ded by the sum of the previous the numbers. However, you should never move backwards. For example, at first, you stay at the "2" cell, and you must move to the "6" cell, then has both selections "8" or "4" be Cause (2+6)/8=1 and (2+6)/4=2, you can not move back to the ' 2 ' cell at this step although (2+6)/2=4. One Possilbe solution is 2->6->8->7->5->3->4->7->11->2->13, and the total number of moves I S 10.
Another solution are also legal but have longer moves:
2->6->8->7->5->3->4->7->5->3->4->7->11->2->13

Input

Thare is at the most cases. The first line of all case have three integers n<=10, S and T, which N indicates the dimension of the map, s and T Indi Cate the number in the start and target cell. Then follows n lines and all line have N positive integers which indicate each number in the n*n cells.
There have one blank line after each case and you can assume, the total number of all cells is no larger than 1000000.

The inputs is ended with End of File. If you have some questions, please visit the help page.

Output

Each case outputs the fewest number of moves or "impossible" if you can not reach the target cell per line.

Sample Input

3 2 13
6 4 3
8 7 5
2 11 2

Sample Output

Test instructions

Give a map of this, from the top left corner of the 2, the goal is to go to the lower right corner of the 13, walking time, requirements: 1-->2, when 2-->3, requires 1 and 22 values added and divisible by 3 that point value, and can not return to go, is to go 3, you can not go back to 2. (This condition is very important, otherwise there is a bug)

Idea: Very simple, direct BFS, at the same time record a FA value, indicating that this point is coming from that point, it can be.

And then do the infinite WA, and then I put the path output, and the same example, I think there is no possibility of wrong AH. Then just think of a set of data, is the data of the dead loop, I do not judge. Then my practice is, for a 10*10 array, although you can repeat some steps, but the expansion of the number of steps is also limited, 900 steps, but also to continue to go is the cycle of death, changed after actually passed. Originally my array opened too small, only 100, after crossing the border, modified the other memory, and then did not report re but WA. have been looking for a long time bug. Alas.. But I actually found the code on the Internet, not my group of dead Loop data (online code RE), then the code is AC. ~~~~~!!!!!!

3 1 100
1 1 1
1 1 1
1 1 1

#include <cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineINF (1&LT;&LT;28)typedefLong Long intLL; #include<iostream>#include<sstream>#include<vector>#include<Set>#include<map>#include<queue>#include<string>#include<conio.h>intn,s,t;Const intmaxn= A;intA[MAXN][MAXN];structdata{intx, y; intcount; intFA;};intnext[4][2]={{0,1},{1,0},{0,-1},{-1,0}};intBFS (intBxintByintEndX,intEndy) {    structData que[ -*Ten+ -]={0};//This array is a little bigger.    intHead,tail; Head=tail=1; intflag=0; que[0].fa=-1; que[0].x=x; que[0].y=by;//the first thing to do is turn in that lattice .Que[tail].count=1;//The first step is fixed, from the upper left corner that comesQue[tail].fa=0; que[tail].x=2; Que[tail].y=2; Tail++; Flag++;//Mark took a few steps to prevent a dead loop     while(head<tail) {         for(intI=0;i<4; i++) {flag++; if(flag== the)//There 's not going to be more than 900 steps.            {                returninf; }            inttx=que[head].x+next[i][0]; intty=que[head].y+next[i][1]; //(tx==que[que[head].fa].x && ty==que[que[head].fa].y//judgment cannot be returned away, must be judged            if(tx>=2&&tx<=n+2&&ty>=2&&ty<=n+2&&! (tx==que[que[head].fa].x && ty==que[que[head].fa].y)) {                intx=que[que[head].fa].x; inty=que[que[head].fa].y; intadd=a[que[head].x][que[head].y]+A[x][y]; if(add%a[tx][ty]==0)                {                    //printf ("%d%d%d\n", add,tx,ty); //getch ();que[tail].x=TX; Que[tail].y=Ty; QUE[TAIL].FA=Head; Que[tail].count=que[head].count+1; if(Tx==endx && ty==Endy) {                        returnQue[tail].count; } Tail++; }}} head++; }    returninf;}voidWork () { for(intI=1; i<=maxn-1; i++)    {         for(intj=1; j<=maxn-1; j + +) {A[i][j]=inf; }    }//set all to INF//I moved the whole map right down one grid.a[2][1]=s;  for(intI=2; i<=n+1; i++)    {         for(intj=2; j<=n+1; j + +) {scanf ("%d",&A[i][j]); }} a[n+1][n+2]=T; /*for (int i =1;i<=n+2;i++) {for (int j=1;j<=n+2;j++) {printf ("%10d", A[i][j]);    } printf ("\ n"); }*/    intANS=BFS (2,1, n+1, n+2); if(ans==inf) {printf ("impossible\n"); }    Elseprintf ("%d\n", ans); return ;}intMain () {#ifdef local freopen ("Data.txt","R", stdin); #endif     while(SCANF ("%d%d%d", &n,&s,&t)! =EOF)    {work (); }    return 0;}
View Code

Although it is past, recall, there is a place worth thinking about, that is, why can't go back. At first I thought I was wrong, and then I couldn't make it back. WA's data, now with strict mathematical proof, go back to WA.

Suppose we go to Y and then (x+y)%targe!=0① (that is, in Y that lattice, coming from X, we can't reach the target)

But can go to Z ==> (x+y)%z==0②

Suppose, suppose, that we can go back and walk back to Y, there (z+y)%y==0③

Then, we can reach the goal (this will WA) (z+y)%targe==0

Now we solve a group of x, Y, Z

By the ② type has, x+y=k1*z; ==> (combined with the following) x= (k1*k2-k1-1) Y;

By the ③ type has, z+y=k2*y; ==> z= (k2-1) *y;

Take k1=7 k2=3

Solve the

X=13y

Y=y

z=2y

So our targe is 3Y.

So that can't go back away is necessary to have data pits your

I am a freshman, if there is where the wrong place, but also ask readers to point out, I am grateful!

Taizhou OJ 3709:number Maze (array out of bounds do not report re, report wa Pit dad)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.