Toyota Programming Contest 2023#4(AtCoder Beginner Contest 311)——D

发布时间 2023-07-23 15:11:09作者: 翔村亲亲鸟

https://atcoder.jp/contests/abc311/tasks/abc311_d

思路

题目说如果当前方向的下一个点能走,那么就一直走,否则才能转向。根据题意模拟即可,这道题的难点在于,碰到已经走过的点到底要不要走。如果当前方向走到底,其中的点之前全部都走过那么就不能再走了。
我们用bfs模拟,对于能一直走的点,while循环走。然后走到底的点加入队列。

代码

#include<bits/stdc++.h>
#define int long long
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define pushk push_back
using namespace std;
const int N = 200+9;
char s[N][N];
int n,m,ans;
bool vis[N][N];
struct node {
	int x,y;
};
int dx[5]= {-1,1,0,0};
int dy[5]= {0,0,-1,1};
signed main() {
	cin>>n>>m;
	rep(i,1,n) {
		cin>>s[i]+1;
	}
	queue<node> q;
	q.push({2,2});
	vis[2][2]=1;
	while(q.size()) {
		auto tmp = q.front();
		q.pop();
		for(int i=0; i<4; ++i) {
			int nx = tmp.x+dx[i];
			int ny = tmp.y+dy[i];
			int cnt=0;
			while((nx>1&&ny>1&&nx<n&&ny<m) && s[nx][ny]=='.') {
				if(!vis[nx][ny]) cnt++;
				vis[nx][ny]=1;
				nx=nx+dx[i];
				ny=ny+dy[i];
			}
			nx-=dx[i],ny-=dy[i];
			if(cnt) {
				q.push({nx,ny});
			}
		}
	}
	rep(i,1,n) {
		rep(j,1,m) {
			if(vis[i][j]) ans++;
		}
	}
	cout<<ans;
	return 0;
}
/*
5 6
######
#oooo#
#o#oo#
oo#ooo
######

*/