Live2D

01迷宫

非AC代码,TLE3个点

题目描述

有一个仅由数字0与1组成的n×n格迷宫。若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上。

你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身)。

输入输出格式

输入格式:

输入的第1行为两个正整数n,m。

下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格。

接下来m行,每行2个用空格分隔的正整数i,j,对应了迷宫中第i行第j列的一个格子,询问从这一格开始能移动到多少格。

输出格式:

输出包括m行,对于每个询问输出相应答案。

#include<iostream>
#include<cstdio>
using namespace std;
struct wz{
	int x,y,z,bnt;
}dl[160005];
struct as{
	int lt,tt;
}ltt[1005][1005];;
int a,b,c,d,i,j,k,l,m,n,tzz,s,t,cnt;
char lk;
void search(int ap,int bp)
{
	if(ltt[ap][bp].lt==1)
	{
		if(ap+1<=n)
		{
			if(ltt[ap+1][bp].lt==0&<t[ap+1][bp].tt==0)
			{
				cnt++;
				ltt[ap+1][bp].tt=1;
				search(ap+1,bp);
			}
		}
		if(ap-1>=1)
		{
			if(ltt[ap-1][bp].lt==0&<t[ap-1][bp].tt==0)
			{
				cnt++;
				ltt[ap-1][bp].tt=1;
				search(ap-1,bp);
			}
		}
		if(bp+1<=n)
		{
			if(ltt[ap][bp+1].lt==0&<t[ap][bp+1].tt==0)
			{
				cnt++;
				ltt[ap][bp+1].tt=1;
				search(ap,bp+1);
			}	
		}
		if(bp-1>=1)
		{
			if(ltt[ap][bp-1].lt==0&<t[ap][bp-1].tt==0)
			{
				cnt++;
				ltt[ap][bp-1].tt=1;
				search(ap,bp-1);
			}	
		}
		
	}
	if(ltt[ap][bp].lt==0)
	{
		if(ap+1<=n)
		{
			if(ltt[ap+1][bp].lt==1&<t[ap+1][bp].tt==0)
			{
				cnt++;
				ltt[ap+1][bp].tt=1;
				search(ap+1,bp);
			}
		}
		if(ap-1>=1)
		{
			if(ltt[ap-1][bp].lt==1&<t[ap-1][bp].tt==0)
			{
				cnt++;
				ltt[ap-1][bp].tt=1;
				search(ap-1,bp);
			}
		}
		if(bp+1<=n)
		{
			if(ltt[ap][bp+1].lt==1&<t[ap][bp+1].tt==0)
			{
				cnt++;
				ltt[ap][bp+1].tt=1;
				search(ap,bp+1);
			}	
		}
		if(bp-1>=1)
		{
			if(ltt[ap][bp-1].lt==1&<t[ap][bp-1].tt==0)
			{
				cnt++;
				ltt[ap][bp-1].tt=1;
				search(ap,bp-1);
			}	
		}
		
	}
}
int main()
{
	cin>>n>>m;
	for(a=1;a<=n;a++)
	{
		for(b=1;b<=n;b++)
		{
			cin>>lk;
			if(lk=='0')
			{
				ltt[a][b].lt=0;
			}
			else
			{
				ltt[a][b].lt=1;
			}
		}
	}
	cnt=1;
	for(a=1;a<=m;a++)
	{
		cin>>s>>t;
		ltt[s][t].tt=1;
		search(s,t);
		cout<<cnt<<endl;
		cnt=1;
		for(c=1;c<=n;c++)
		{
			for(b=1;b<=n;b++)
			{
				ltt[c][b].tt=0;
			}
		}
	}
}


posted @ 2017-12-04 15:58  ztz11  阅读(151)  评论(0编辑  收藏  举报