AtCoder Beginner Contest 311 - D(搜索)
ABC 简单题
D 搜索
D - Grid Ice Floor
题意
给定一个
问你玩家能够经过多少个位置 '.' ?
思路
刚开始的思路是搜索,但是搜索有个问题,就是可能会漏,因为每个位置是可以重复到达的,为了使得玩家可以到达尽可能多的未到达的位置。
所以除去搜索的思路,我们利用一个队列来不重地记录每次停下的位置,并再这个位置向四个方向延伸,以此来找遍所有的可达位置,同时记录答案即可。
代码
//>>>Qiansui #include<bits/stdc++.h> #define ll long long #define ull unsigned long long #define mem(x,y) memset(x, y, sizeof(x)) #define debug(x) cout << #x << " = " << x << '\n' #define debug2(x,y) cout << #x << " = " << x << " " << #y << " = "<< y << '\n' //#define int long long using namespace std; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; /* */ const int maxm = 2e2 + 5, inf = 0x3f3f3f3f, mod = 998244353; int n, m, ans, fx[4] = {1, 0, -1, 0}, fy[4] = {0, -1, 0, 1}; bool vis[maxm][maxm], pos[maxm][maxm]; string ss[maxm]; void solve(){ cin >> n >> m; for(int i = 1; i <= n; ++ i){ cin >> ss[i]; ss[i] = 'a' + ss[i]; } vis[2][2] = true; queue<pii> q; q.push({2, 2}); while(!q.empty()){ auto [x, y] = q.front(); q.pop(); if(pos[x][y]) continue; vis[x][y] = true; pos[x][y] = true; int xx, yy; for(int i = 0; i < 4; ++ i){ xx = x; yy = y; while(1){ xx += fx[i]; yy += fy[i]; if(ss[xx][yy] == '.'){ vis[xx][yy] = true; }else{ xx -= fx[i]; yy -= fy[i]; if(!pos[xx][yy]) q.push({xx, yy}); break; } } } } for(int i = 1; i <= n; ++ i){ for(int j = 1; j <= m; ++ j){ if(vis[i][j]) ++ ans; } } cout << ans << '\n'; return ; } signed main(){ ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int _ = 1; // cin >> _; while(_ --){ solve(); } return 0; }
本文来自博客园,作者:Qiansui,转载请注明原文链接:https://www.cnblogs.com/Qiansui/p/17638999.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现