Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

明明数据都过了但还是WA

Posted by kigoemasuga at 2017-02-15 19:24:15
#include <iostream>
#include <memory.h>
using namespace std;
int maze[20][20];
int startx, starty;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,-1,0,1};
int sum;
int ay, ax;


void dfs(int x, int y, int move) {
	if (move > 10) return;
	for (int i = 0; i < 4; i++) {
		int tempx = x + dx[i];
		int tempy = y + dy[i];
		int step = 0;
		while (tempx>=0 && tempx<ax && tempy>=0 && tempy<ay && maze[tempx][tempy] == 0) {
			tempx = tempx + dx[i];
			tempy = tempy + dy[i];
			step++;
		}
		if (maze[tempx][tempy] == 3 && sum > move) {
			sum = move;
		}
		else if (maze[tempx][tempy] == 1 && step>0) {
			// memcpy(nextMaze, tempMaze, sizeof(int) * 20 * 20); -
			maze[tempx][tempy] = 0;
			dfs(tempx - dx[i], tempy - dy[i], move + 1);
			maze[tempx][tempy] = 1;		//+
		}
	}
}


int main()
{
	while (cin>>ay>>ax&&ax) {
		sum = 11;
		memset(maze,-1,sizeof(maze));
		for (int i = 0; i < ax; i++) {
			for (int j = 0; j < ay; j++) {
				cin >> maze[i][j];
				if (maze[i][j] == 2) {
					startx = i;
					starty = j;
					maze[i][j] = 0;	//+
				}
			}
		}
		dfs(startx, starty, 1);
		if (sum == 11)cout << -1 << endl;
		else cout << sum << endl;
	}
    return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator