[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; }
相关文章推荐
- 数据结构经典算法学习之老鼠走迷宫01
- 洛谷P1141 01迷宫
- 洛谷P1141 01迷宫
- P1141 01迷宫
- 洛谷P1141 01迷宫
- luogu P1141 01迷宫
- python数据结构学习笔记-2017-01-08-01-N皇后问题、迷宫问题和跳马问题的递归解决
- 寒假17:迷宫问题01,能否走出去
- 01迷宫
- 【u115】&&【t031】 01迷宫
- 洛谷P1141 01迷宫
- 迷宫问题 192132-01
- Luogu P1141 01迷宫【搜索/dfs】By cellur925
- 01迷宫
- 洛谷Oj-01迷宫-广度优先搜索
- [luogu]P1141 01迷宫-宽搜
- luogu P1141 01迷宫 x
- P1141 01迷宫
- [LUOGU]1141 01迷宫
- 洛谷P1141 01迷宫