poj 1154
来源:互联网 发布:飞鸽传书网络打印机 编辑:程序博客网 时间:2024/06/05 18:25
第一阶段未懂回溯#include <iostream>#include <fstream>#include <cstring>using namespace std;const int MAXN=30;char maze[MAXN][MAXN];int vis[MAXN][MAXN];int counter=0;int letter[26];void DFS(int x,int y){ if(vis[x][y] || maze[x][y]=='#' || letter[maze[x][y]-65]==1)return ; vis[x][y]=1; counter++; letter[maze[x][y]-65]=1; DFS(x-1,y); DFS(x,y-1); DFS(x+1,y); DFS(x,y+1);}int main(){ //cout << "Hello world!" << endl; //freopen("input.txt","r",stdin); int R,C; while(cin>>R>>C) { memset(vis,0,sizeof(vis)); memset(maze,'#',sizeof(maze)); memset(letter,0,sizeof(letter)); for(int i=1;i<=R;i++) { for(int j=1;j<=C;j++) { cin>>maze[i][j]; } } DFS(1,1); cout<<counter<<endl; //output maze /* for(int i=0;i<=R+1;i++) { for(int j=0;j<=C+1;j++) { cout<<maze[i][j]; } cout<<endl; }*/ //fclose(stdin); } return 0;}
第二阶段应用回溯
#include <iostream>#include <fstream>#include <cstring>#include <cstdio>using namespace std;const int MAXN=30;char map[MAXN][MAXN];int vis[MAXN][MAXN];int letter[26];int sum=1;int total=0;int Dir_x[4]={0,1,0,-1};int Dir_y[4]={-1,0,1,0};int R,C;void DFS(int x,int y){ vis[x][y]=1; int nextx,nexty; if(sum>total)total=sum; for(int i=0;i<4;i++) { nextx=x+Dir_x[i]; nexty=y+Dir_y[i]; if(nextx>=0 && nextx<R && nexty>=0 && nexty<C && !letter[map[nextx][nexty]-'A'] && !vis[nextx][nexty]) { letter[map[x][y]-'A']=1; sum++; DFS(nextx,nexty); sum--; letter[map[x][y]-'A']=0; vis[x][y]=0; } }}int main(){ //cout << "Hello world!" << endl; //freopen("input.txt","r",stdin); //while(scanf("%d %d",&R,&C)==2) cin>>R>>C; { //if(R==0 || C==0)break; memset(letter,0,sizeof(letter)); memset(vis,0,sizeof(vis)); //memset() for(int i=0;i<R;i++) { for(int j=0;j<C;j++) { cin>>map[i][j]; } } DFS(0,0); cout<<total<<endl; } //fclose(stdin); return 0;}
0 0
- POJ 1154
- poj 1154
- poj 1154
- POJ ACM 1154
- poj 1154 LETTERS
- poj 1154 dfs
- POJ 1154 LETTERS
- poj 1154 LETTERS
- poj 1154 LETTERS
- poj - 1154 - LETTERS
- poj LETTERS 1154
- poj 1154 LETTERS
- POJ 1154 LETTERS
- poj 1154dfs水
- poj 1154 LETTERS
- POJ 1154 解题报告
- poj 1154 LETTERS
- POJ 1154 LETTERS 笔记
- 带时间的日期选择web控件
- 我的首个电子书软件--嘎嘎读书 的开发(三)
- ZIp 解压
- Ubuntu汉化
- android动态布局方法小结
- poj 1154
- Opengl ES 2.0 简单的纹理范例 iOS
- oracle job详解
- cmd使用
- C#完整的通信代码(点对点,点对多,同步,异步,UDP,TCP)
- lr_start_transaction和lr_end_transaction
- 算法
- TabHost的用法
- linux之 grep命令