hdu 1312(艰辛的debug之路/其实题目很简单)
来源:互联网 发布:数据库的基本对象是 编辑:程序博客网 时间:2024/04/29 05:13
题目大意:
一个站在一个用正方形地板铺成的房间内,地板的颜色为红色和黑色,此人不能站到红色的地板上,只能站到黑色的地板上;‘@’为此人的初始位置,'#'表示红地板,‘.’表示黑地板,此人每次只能移到相邻的地板上,不能斜着移动,求出此人可移动到的地板数量。
坑爹的题目,题意很好理解,只要搜索遍历就行了,可是偏偏是那坑爹的坐标(行、列搞晕了,调了好长时间的代码,愣是没找到错误(╯﹏╰)),不过最后总算找到了,这道题深搜、广搜都行,建议用不熟练的练手(个人感觉广搜简单省时)
一定要注意坐标,不要下次再掉到这个坑里。。。。。。
//广搜#include<cstdio>#include<cstring>#include<iostream>#define N 25using namespace std;char map[N][N];int dir[4][2]={1,0,0,1,-1,0,0,-1},w,h;struct point {int x, y;}S;int main(){int bfs();while(scanf("%d%d",&w,&h)==2&&w!=0&&h!=0){int i,j;for(i=0;i<h;i++)for(j=0;j<w;j++){scanf(" %c",&map[i][j]);if(map[i][j]=='@'){S.x=i;S.y=j;}}printf("%d\n",bfs());/*for(i=0;i<h;i++){for(j=0;j<w;j++)printf("%c",map[i][j]);printf("\n");}*/}return 0;}int bfs(){point q[10000];int count=0;point cur,next;q[0]=S;//printf("%d %d\n",q[0].x,q[0].y);map[S.x][S.y]='#';int fro=0,aft=1;while(fro<aft){cur=q[fro];++fro;for(int i=0;i<4;i++){int tx,ty;next.x=cur.x+dir[i][0];next.y=cur.y+dir[i][1];if(map[next.x][next.y]=='.'&&next.x<h&&next.y<w&&next.x>=0&&next.y>=0){//printf("%d %d\n",next.x,next.y);q[aft++]=next;map[next.x][next.y]='#';}}}return aft;}
//深搜#include<cstdio>#include<iostream>#include<cstring>using namespace std; char maps[25][25];int r,c,sx,sy,res;int dx[]={-1,1,0,0};int dy[]={0,0,-1,1}; void dfs(int a,int b){ for(int i=0;i<4;i++) { int xx=a+dx[i]; int yy=b+dy[i]; if(xx>=1&&xx<=r&&yy>=1&&yy<=c&&maps[xx][yy]=='.') { maps[xx][yy]='#'; res++; dfs(xx,yy); } }} int main(){ int i,j; while(scanf("%d%d",&c,&r)&&r&&c) { memset(maps,0,sizeof(maps)); res=1; for(i=1;i<=r;i++) { for(j=1;j<=c;j++) { scanf(" %c",&maps[i][j]); if(maps[i][j]=='@') { sx=i; sy=j; maps[i][j]='#'; } } } dfs(sx,sy); printf("%d\n",res); } return 0;}
*************************************************************************************************************************************************************************
补充:转自Sinchb运用了类似种子填充算法,使用了栈的思想
算法分析
本题要求包含@的连通区域,跟图形学中种子填充算法类似,采用堆栈,将@所在位置作为种子,依次填充该种子前后左右,直到堆栈为空。
代码如下:<span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">(采用GCC提交的话,不要包含<malloc.h><memory.h>等头文件)</span>
#include <stdio.h>#include <string.h>//#include <malloc.h>struct dirNode{ unsigned x; unsigned y; struct dirNode * next;};struct stack{ struct dirNode *top;};struct stack * Stack_init(){ struct stack *p; p = (struct stack *)malloc(sizeof(struct stack)); if(p != NULL) p->top = NULL; return p;}struct stack * Stack_insert(struct stack *s,int x,int y){ struct dirNode *p; p = (struct dirNode *)malloc(sizeof(struct dirNode)); if(p != NULL) { p->x = x; p->y = y; p->next = NULL; } p->next = s->top; s->top = p; return s;}struct stack * Stack_pop(struct stack *s){ struct dirNode *p; if (s == NULL || s->top == NULL) return s; p = s->top; s->top = p->next; free(p); return s;}int main(){ int W,H,i,j,room[20][20],res = 0,ii,jj,tmpi,tmpj; short cal[4][2] = {-1,0,0,+1,+1,0,0,-1}; char tmp; struct dirNode *stack = NULL; while(scanf("%d%d",&W,&H) && W && H) { res = 0; struct stack *s = Stack_init(); getchar(); for( i = 0 ; i < H ;i++) { for( j = 0 ; j < W; ) { tmp = getchar(); room[i][j] = (tmp == '#') ? 0 : 1; if (tmp == '@') { s = Stack_insert(s,i,j); room[i][j] = 0; res += 1; } j++; } getchar(); } while(s->top!= NULL) { ii = s->top->x; jj = s->top->y; s = Stack_pop(s); for(i = 0;i < 4 ;i++) { tmpi = ii + cal[i][0]; tmpj = jj + cal[i][1]; if( (tmpi < H)&&(tmpi >=0) &&(tmpj < W) && (tmpj >=0 )) { if(room[tmpi][tmpj] == 1) { room[tmpi][tmpj] = 0; s = Stack_insert(s,tmpi,tmpj); res++; } } } } printf("%d\n",res); } //system("pause"); return 0;}
0 0
- hdu 1312(艰辛的debug之路/其实题目很简单)
- 艰辛的程序员之路!
- hdu 2042 不容易系列之二(其实非常简单)
- 任何职位其实都很艰辛。
- 艰辛的aptana插件安装之路
- 艰辛坎坷的360崛起之路
- 程序员找工作的艰辛之路啊!
- Theano-艰辛的GPU配置之路
- linux配置MySQL的艰辛之路
- 艰辛的考研路
- Block 其实很简单的
- 我的八年程序之路(一)求职的艰辛
- 《财富》:戴尔的恢复之路为何如此艰辛
- 从一个技校毕业生到高级程序员的艰辛之路
- ubuntu-16.04-desktop-amd64的艰辛安装之路
- 发布Library到jcenter的艰辛之路
- 我的ACM艰辛之路,求罩
- 一个站长找回域名的艰辛之路
- hdu1885
- hdu 2119 Matrix 二分图 最小点覆盖
- 6个海盗抢到了100颗宝石,每一颗都一样的大小和价值连城。他们决定这么分:
- git配置和简单使用
- Qt 文件读写
- hdu 1312(艰辛的debug之路/其实题目很简单)
- 隐藏TabBar的一些方法小结
- linux里的backlog详解
- 192.0x00c8.0x00c8.0x00fa
- hadoop云盘客户端的设计与实现(三)
- [iOS diary]UIScrollView或UITextView 滑动部分无故向下偏移,contentOffset增加64
- leetcode:Largest Rectangle in Histogram
- LeetCode OJ算法题(十八):Letter Combinations of a Phone Number
- Leetcode优化:Convert a binary tree to linked list