UVALive 4622 Decision(bfs)
来源:互联网 发布:网络电视为什么不清楚 编辑:程序博客网 时间:2024/05/16 17:31
题目链接:【UVALive 4622】
ABCDEF代表着6种不同的 区块,输入n*m的由A~F组成的字符矩阵,问这个矩阵有几个阴影区块,有公共边的是相连的
从边入手,用结构体储存四条边,有阴影的边是1,无阴影的边是0,比如B这个图像,n.l=1, n.d=1, n.r=0,,n.f=0,bfs搜索,将公共边都是1的标记
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <string>#include <queue>using namespace std;int t, n, m;char str[1010][1010];int vis[1010][1010];int f[4][2] = {0,1,1,0,0,-1,-1,0};struct node{int l, d, r, f, x, y;}p[1010][1010], s, e;void getp(int i, int j){if(str[i][j]=='A') p[i][j].l=0, p[i][j].d=0, p[i][j].r=0, p[i][j].f=0, p[i][j].x=i, p[i][j].y=j; else if(str[i][j]=='B') p[i][j].l=1, p[i][j].d=1, p[i][j].r=0, p[i][j].f=0, p[i][j].x=i, p[i][j].y=j;else if(str[i][j]=='C') p[i][j].l=1, p[i][j].d=0, p[i][j].r=0, p[i][j].f=1, p[i][j].x=i, p[i][j].y=j;else if(str[i][j]=='D') p[i][j].l=0, p[i][j].d=0, p[i][j].r=1, p[i][j].f=1, p[i][j].x=i, p[i][j].y=j;else if(str[i][j]=='E') p[i][j].l=0, p[i][j].d=1, p[i][j].r=1, p[i][j].f=0, p[i][j].x=i, p[i][j].y=j;else if(str[i][j]=='F') p[i][j].l=1, p[i][j].d=1, p[i][j].r=1, p[i][j].f=1, p[i][j].x=i, p[i][j].y=j;}void bfs(int xi, int yi, int li){queue<node>q;s = p[xi][yi];s.x=xi, s.y=yi;q.push(s);while(!q.empty()){s = q.front(), q.pop();if(s.l){e = s;e.y--;e = p[e.x][e.y];if(e.r&&!vis[e.x][e.y]&&(e.x>=1&&e.x<=n&&e.y>=1&&e.y<=m)) {vis[e.x][e.y] = li;q.push(e);}}if(s.d){e = s;e.x++;e = p[e.x][e.y];if(e.f&&!vis[e.x][e.y]&&(e.x>=1&&e.x<=n&&e.y>=1&&e.y<=m)) {vis[e.x][e.y] = li;q.push(e);}}if(s.r){e = s;e.y++;e = p[e.x][e.y];if(e.l&&!vis[e.x][e.y]&&(e.x>=1&&e.x<=n&&e.y>=1&&e.y<=m)) {vis[e.x][e.y] = li;q.push(e);}}if(s.f){e = s;e.x--;e = p[e.x][e.y];if(e.d&&!vis[e.x][e.y]&&(e.x>=1&&e.x<=n&&e.y>=1&&e.y<=m)) {vis[e.x][e.y] = li;q.push(e);}}}}int main(){scanf("%d", &t);while(t--){scanf("%d%d", &n, &m);for(int i=1; i<=n; i++){scanf("%s", str[i]+1);for(int j=1; j<=m; j++){getp(i, j);}}int li=0;memset(vis, 0, sizeof(vis));for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){if(vis[i][j]) continue;vis[i][j] = 1;if(str[i][j]=='A') continue;li++;bfs(i, j, li);}}printf("%d\n", li);}return 0;}
0 0
- UVALive 4622 Decision(bfs)
- UVALive 6930 Wheels(bfs)
- UVALive 7155 Keyboarding(BFS)
- UVALive 5006 (BFS+heap+01背包)
- UVALive 6854City(bfs + 容斥)
- UVALive 6425 Intercity(bfs)
- UVALive 6658 - Fiasco(BFS)
- UVALive 3695 (博弈 bfs)
- 【BFS && 树】UVALive
- UVALive 5066 - Fire Drill (BFS+01背包)
- UVALive 6665 Dragon’s Cruller (BFS + 优先队列+hash)
- UVALive 4627 Islands(BFS+并查集)
- 【UVALive】2147 - Push!!(bfs+dfs+优先队列)
- UVALive 7263 Today Is a Rainy Day(BFS预处理)
- UVALive - 2093 Moving Pegs bfs
- UVALive - 3977 Summits (BFS染色)
- uvalive 6693 - Flow Game - BFS
- 决策树(Decision Tree)
- 圆形圆角图片
- Java程序和垃圾回收机制
- 1Z0-050 question 2
- 给软件添加注册码【笔记】
- java利用反射机制获取实体类属性自定义注解说明
- UVALive 4622 Decision(bfs)
- Java中的构造方法总结
- 初窥Linux 之 我最常用的20条命令
- wap 前端开发--禁止页面滚动Android版 touchmove
- View controller-based status bar appearance
- 自定义标签TagSupport
- 深入浅出外观模式(三):抽象外观类,外观模式效果与适用场景
- Startup Error: Unable to detect graphics environment
- 天声人語 20160127 言葉を友人に持とう