#include <stdio.h>#defineROCK 1#defineGOAL 3intmap[ -][ -];intw,h;intDfsfind (intStartXintStarty,intFinx,intFiny,intDeep ) { if(Deep >Ten) return-1; intMin = One; //move to the left. if(StartX >0&& Map[startx-1][starty]! =ROCK) { intTMPX =StartX; while(Tmpx >0&& MAP[TMPX-1][starty]! =ROCK) { if(MAP[TMPX-1][starty] = =GOAL) { return 1; } tmpx--; } //If there is a stone if(TMPX! =0) { intRockx = tmpx-1; intRocky =Starty; Map[rockx][rocky]=0; intTmpmin = Dfsfind (tmpx, Starty, Finx, finy, deep +1) +1; Map[rockx][rocky]=ROCK; if(Tmpmin >0&& min >tmpmin) {min=tmpmin; } } } //move to the right. if(StartX < H-1&& Map[startx +1][starty]! =ROCK) { intTMPX =StartX; while(Tmpx < H-1&& map[tmpx +1][starty]! =ROCK) { if(MAP[TMPX +1][starty] = =GOAL) { return 1; } tmpx++; } //If there is a stone if(tmpx! = h-1) { intRockx = tmpx +1; intRocky =Starty; Map[rockx][rocky]=0; intTmpmin = Dfsfind (tmpx, Starty, Finx, finy, deep +1) +1; Map[rockx][rocky]=ROCK; if(Tmpmin >0&& min >tmpmin) {min=tmpmin; } } } //Move upwards if(Starty >0&& Map[startx][starty-1] !=ROCK) { intTmpy =Starty; while(Tmpy >0&& Map[startx][tmpy-1] !=ROCK) { if(Map[startx][tmpy-1] ==GOAL) { return 1; } tmpy--; } //If there is a stone if(Tmpy! =0) { intRockx =StartX; intRocky = tmpy-1; Map[rockx][rocky]=0; intTmpmin = Dfsfind (StartX, Tmpy, Finx, finy, deep +1) +1; Map[rockx][rocky]=ROCK; if(Tmpmin >0&& min >tmpmin) {min=tmpmin; } } } //Move Down if(Starty < W-1&& Map[startx][starty +1] !=ROCK) { intTmpy =Starty; while(Tmpy < W-1&& Map[startx][tmpy +1] !=ROCK) { if(Map[startx][tmpy +1] ==GOAL) { return 1; } tmpy++; } //If there is a stone if(Tmpy! = W-1) { intRockx =StartX; intRocky = Tmpy +1; Map[rockx][rocky]=0; intTmpmin = Dfsfind (StartX, Tmpy, Finx, finy, deep +1) +1; Map[rockx][rocky]=ROCK; if(Tmpmin >0&& min >tmpmin) {min=tmpmin; } } } returnMin < One? Min:-1;}intMainvoid){ intStartx,starty; intFinx,finy; while(SCANF ("%d%d", &w, &h)) {if(W = =0&& h = =0) Break; for(inti =0; I < H; i + +) { for(intj =0; J < W; J + +) {scanf ("%d", &Map[i][j]); if(Map[i][j] = =2) {StartX=i; Starty=J; } Else if(Map[i][j] = =3) {Finx=i; Finy=J; } } } intres = Dfsfind (startx, Starty, Finx, Finy,0); printf ("%d\n", RES); } return 0;}
Occupy a pit sticker code, tomorrow to work to write