HDU 1312 Red and Black 题解

复制代码
//注意边界判断,调了好久

#include <iostream> #include <queue> using namespace std; #define check(x,y)(x<wx&&x>=0&&y<hy&&y>=0) struct node { int x, y; }; char room[23][23]; int n, m, wx, hy, num; int dir[4][2] = { {-1, 0}, {0, -1}, {1, 0}, {0, 1} }; void bfs(int dx, int dy) { num = 1; queue<node>q; node start, next; start.x = dx; start.y = dy; q.push(start); while (!q.empty()) { start = q.front(); q.pop(); // cout<<"out"<<start.x<<' '<<start.y<<endl; for (int i = 0; i < 4; i++) { next.x = start.x + dir[i][0]; next.y = start.y + dir[i][1]; if (check(next.x, next.y) && room[next.x][next.y] == '.') { room[next.x][next.y] = '#'; num++; q.push(next); } } } } int main() { int x, y, dx, dy; while (cin >> wx >> hy) { if (wx == 0 && hy == 0) { break; } for ( y = 0; y < hy; y++) { for (int x = 0; x < wx; x++) { cin >> room[x][y]; if (room[x][y] == '@') { dx = x; dy = y; } } } num = 0; bfs(dx, dy); cout << num << endl; } return 0; }
复制代码

 

posted @   Miya555  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示
主题色彩