您的位置:首页 > 其它

[luogu1141 01迷宫]

2016-10-16 11:07 197 查看
题目链接

题解:记忆化

#include <iostream>
#include <cstdio>
using namespace std;
const int M=1005;
int n,m,x,y;
int map[M][M],ans[M*100];
int vis[M][M];
char s[M][M];
bool check(int i,int j,int z){
if(i>n||j>n||i<1||j<1||map[i][j]!=z||vis[i][j]) return 0;
return 1;
}
void dfs(int x,int y,int z,int T)
{
if(!check(x,y,z)) return ;
vis[x][y]=T;ans[T]++;
dfs(x+1,y,1-z,T);dfs(x,y+1,1-z,T);
dfs(x-1,y,1-z,T);dfs(x,y-1,1-z,T);
}
void init()
{
cin>>n>>m;
for(int i=0;i<n;i++)
scanf("%s",s[i]);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=s[i-1][j-1]-'0';
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
if(!vis[x][y]) dfs(x,y,map[x][y],i);
else ans[i]=ans[vis[x][y]];
}
}
void work()
{
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
int main()
{
//freopen("data.out","r",stdin);
//freopen("AC.out","w",stdout);
init();
work();
//fclose(stdin);fclose(stdout);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: