LightOJ 1012(基础图bfs)
来源:互联网 发布:企业信用基础数据库 编辑:程序博客网 时间:2024/06/06 02:22
我写麻烦了,可以由终点反搜。
基础的bfs,debug了好长时间,平时不多打的原因啊,比赛代码都交给队友,导致我代码能力很弱啊。
警惕警惕!
#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;const int N = 30;char s[N][N];int v[N][N];int dx[4] = {1,-1,0,0};int dy[4] = {0,0,1,-1};struct point{ int x,y;};int n,m;int bfs(int x1,int y1){ memset(v,0,sizeof(v)); queue<point> Q; point now; now.x = x1; now.y = y1; v[now.x][now.y] = 1; Q.push(now); while(!Q.empty()) { now = Q.front(); //printf("%d %d\n",now.x,now.y); Q.pop(); for(int i = 0; i < 4; i++) { point next; next.x = now.x+dx[i]; next.y = now.y+dy[i]; //printf(" %d %d %c %d\n",next.x,next.y,s[next.x][next.y],v[next.x][next.y]); if(next.x< 0 || next.x >=m || next.y<0 || next.y>=n) { continue; } if(s[next.x][next.y] == '@') { return 1; } if(v[next.x][next.y] == 0 && s[next.x][next.y]=='.') { v[next.x][next.y] = 1; Q.push(next); } } } return 0;}int main(){ int T,cas = 1; scanf("%d",&T); while(T--) { memset(v,0,sizeof(v)); scanf("%d %d",&n,&m); for(int i = 0; i < m; i++) scanf("%s",s[i]); int ans = 1; for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(s[i][j] == '.') { if(bfs(i,j)) ans ++; } } } printf("Case %d: %d\n",cas++,ans); } return 0;}
0 0
- LightOJ 1012(基础图bfs)
- Lightoj 1141 BFS
- Lightoj 1009 BFS
- LightOJ 1174--Commandos (BFS)
- LightOJ 1175 (BFS)
- LightOJ-1046-BFS
- lightoj 1099【dijkstra/BFS】
- LightOJ-1094-图论,bfs
- LightOJ-1066-bfs
- lightoj-1141【bfs】
- LightOJ - 1150 Ghosts!(二分图+bfs+枚举)
- LeetCode基础-图-BFS
- LightOJ 1141 Number Transformation bfs
- BFS 基础
- lightoj(基础数学)
- lightoj 1150 - Ghosts! 【BFS + 二分查找 + 判二分图完美匹配】
- LightOJ - 1039 A Toy Company(BFS)
- LightOJ 1238 Power Puff Girls(BFS)
- Android Activity任务和返回栈全面解析
- WebView的注意事项和小实例
- 多线程
- 三层架构
- Java运算符的优先级
- LightOJ 1012(基础图bfs)
- App上架流程(IOS)
- 事件
- 数据库
- 第五讲项目2.1-长方形的周长和面积
- 链表之双链表
- 算法笔记:动态规划求解最长子序列问题
- 网络编程
- GIT的使用复习