记录编号 249816 评测结果 AAAAAAAAAA
题目名称 定向越野 最终得分 100
用户昵称 GravatarRiolu 是否通过 通过
代码语言 C++ 运行时间 0.382 s
提交时间 2016-04-13 17:56:51 内存使用 0.43 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<vector>
using namespace std;
int n,e[101][101],ans,book[101][101],flag,timer=0,arrive[101][101];
struct note{int x,y;}at;
vector<note> dian[201];
void dfs(int x,int y){
	if(x>n||x<1||y>n||y<1) {return ;}
	if(x==n&&y==n){
		cout<<ans<<endl;
		exit(0);
		}timer++;
	if(book[x+1][y]==0&&arrive[x+1][y]==0){
		arrive[x+1][y]=1;
		dfs(x+1,y);
		}
		if(book[x-1][y]==0&&arrive[x-1][y]==0){
		arrive[x-1][y]=1;
		dfs(x-1,y);
		}
		if(book[x][y+1]==0&&arrive[x][y+1]==0){
		arrive[x][y+1]=1;
		dfs(x,y+1);
		}
		if(book[x][y-1]==0&&arrive[x][y-1]==0){
		arrive[x][y-1]=1;
		dfs(x,y-1);
		}
	}
int main()
{
	freopen("adven.in","r",stdin);
	freopen("adven.out","w",stdout);
	int i,j,k;
	cin>>n;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++){
			scanf("%d",&e[i][j]);
			at.x=i;at.y=j;
			dian[e[i][j]].push_back(at);
			}
	for(i=0;i<=200;i++){	
		memset(book,-1,sizeof(book));	
		for(k=0;k<=i;k++)
			for(unsigned int it=0;it!=dian[k].size();it++)
				book[dian[k][it].x][dian[k][it].y]=0;				
				memset(arrive,0,sizeof(arrive));
				ans=i;
				if(e[1][1]>=0&&e[1][1]<=i)					
				dfs(1,1);			
		for(j=1;j+i<=200;j++){
			memset(arrive,0,sizeof(arrive));
			for(unsigned int it=0;it!=dian[j-1].size();it++)
				book[dian[j-1][it].x][dian[j-1][it].y]=-1;
			for(unsigned int it=0;it!=dian[j+i].size();it++)
				book[dian[j+i][it].x][dian[j+i][it].y]=0;
		ans=i;
		if(e[1][1]>=j&&e[1][1]<=j+i)
			dfs(1,1);
			
		}
	}

	return 0;
}