mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-10-17 21:42:25 +00:00
feat: 添加P11228.cpp解题代码实现网格遍历算法
实现一个网格遍历算法,根据输入参数在网格中移动并统计访问过的格子数量。包含以下功能: - 读取网格大小和初始位置 - 处理移动方向变化 - 统计并输出访问过的不同格子数量
This commit is contained in:
parent
6e9562d5cb
commit
e8b9924069
50
src/9/26/P11228.cpp
Normal file
50
src/9/26/P11228.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <istream>
|
||||
|
||||
using ll = int64_t;
|
||||
const ll maxn = 1e3+5;
|
||||
bool map[maxn][maxn],vis[maxn][maxn];
|
||||
ll T,n,m,k,x0,y0,d0;
|
||||
const ll dis[4][2] = {
|
||||
{0,1},{1,0},
|
||||
{0,-1},{-1,0}
|
||||
};
|
||||
|
||||
inline static void solve(){
|
||||
std::cin>>n>>m>>k
|
||||
>>x0>>y0>>d0;
|
||||
for(ll i=1;i<=n;i++){
|
||||
for(ll j=1;j<=m;j++){
|
||||
char c;
|
||||
std::cin>>c;
|
||||
map[i][j]= c=='.';
|
||||
vis[i][j]=false;
|
||||
}
|
||||
}
|
||||
ll ans=1;
|
||||
vis[x0][y0]=true;
|
||||
while(k--){
|
||||
ll nx = x0+dis[d0][0],ny=y0+dis[d0][1];
|
||||
if(nx<1 || nx>n || ny<1 || ny>m || !map[nx][ny]){
|
||||
d0=(d0+1)%4;
|
||||
continue;
|
||||
}
|
||||
x0=nx,y0=ny;
|
||||
if(!vis[nx][ny]){
|
||||
ans++;
|
||||
vis[nx][ny]=true;
|
||||
}
|
||||
}
|
||||
std::cout<<ans<<"\n";
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::iostream::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin>>T;
|
||||
while(T--){
|
||||
solve();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user