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

为什么地图不能是20*20的

Posted by silence_42 at 2017-08-03 17:37:24 on Problem 3009
我用board[20][20]来记录地图,结果就WA了,自己试了题目给的数据都是对的

后面看了别人的都是21,25,30的边长,我就改成了21,然后就A了,想知道原因

自己看了下程序在执行的时候也不会产生越界的情况啊(下面的程序已经+1了,没+1的通不过)

#include<cstdio>
#include<algorithm>

using std::lower_bound;
using std::sort;

const int MAX_W=20, MAX_H=20, MAX_DATA_SETS=100;

int h,w,board[MAX_H+1][MAX_W+1];

int sx,sy;

int dx[4]={-1,1,0,0}, dy[4]={0,0,1,-1};

int ans[MAX_DATA_SETS+1],ansCnt;

bool inBoard(int x,int y)
{
	if(x>=0 && x<h && y>=0 && y<w) return 1;
	return 0;
}

int dfs(int x, int y, int t)
{
	if(t==10) return -1;
	int res[4];
	for(int i=0;i<4;i++){
		int nx=x+dx[i], ny=y+dy[i];
		if(!inBoard(nx,ny)|| board[nx][ny]==1) {
			res[i]=-1;
			continue;
		} else {
			while(board[nx][ny]!=1){
				if(board[nx][ny]==3) {
					res[i]=1;
					break;
				}
				nx+=dx[i];
				ny+=dy[i];
				if(!inBoard(nx,ny)) {
					res[i]=-1;
					break;
				}
			}
			if(board[nx][ny]==1){
				board[nx][ny]=0;
				res[i]=dfs(nx-dx[i],ny-dy[i],t+1);
				if(res[i]!=-1) res[i]++;
				board[nx][ny]=1;
			}
		}
	}
	sort(res,res+4);
	int *p=lower_bound(res,res+4,0);
	if(p==res+4) return -1;
	else return *p;
}

int main()
{
	while(1){
		scanf("%d%d",&w,&h);
		memset(board,0,sizeof(board));
		if(!w && !h) break;
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				scanf("%d",&board[i][j]);
				if(board[i][j]==2){
					sx=i;sy=j;
				}
			}
		}
		ans[ansCnt]=dfs(sx,sy,0);
		ansCnt++;
	}

	for(int i=0;i<ansCnt;i++)
		printf("%d\n",ans[i]);
	
	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