刷题日记2

第二周任务 [Cloned] - Virtual Judge (vjudge.net)

思路:这题就是遍历s的四个点,如果是 . 就进行dfs遍历搜索,如果能够达到起点,就说明满足题意,就输出Yes;

#include<bits/stdc++.h>
using namespace std;
#define YES cout<<"YES"<<'\n';
#define NO  cout<<"NO"<<'\n';
#define endl cout<<'\n';
typedef long long ll;
const int maxn=1e6+10,inf = 1e18 ;
const int mod = 1e9 + 7;
using namespace std;
map<int, char>mp[maxn];
map<int, bool>vis[maxn];
int h, w;
int dir[4][2] = { {0,1},{1,0},{-1,0},{0,-1} };
int dx, dy;
bool flag = 0;
void dfs(int x,int y,int dep) {
	vis[x][y] = 1;
	if (x == dx && y == dy ) {
		flag = 1;
		return;
	}
	for (int i = 0; i < 4; i++) {
		int nx = x + dir[i][0];
		int ny = y + dir[i][1];
		if (!vis[nx][ny] && nx >= 1 && nx <= h && ny >= 1 && ny <= w&&mp[nx][ny]!='#') {
			if (nx == dx && ny == dy && dep == 0) continue;
			dfs(nx, ny, dep + 1);
		}
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin >> h >> w;
	 for (int i = 1; i <= h; i++) {
		 for (int j = 1; j <= w; j++) {
			 cin >> mp[i][j];
			 if (mp[i][j] == 'S') {
				 dx = i;
				 dy = j;
			 }
		 }
	 }
	 for (int i = 0; i < 4; i++) {
		 int nx = dx + dir[i][0];
		 int ny = dy + dir[i][1];
		 if (nx >= 1 && nx <= h && ny >= 1 && ny <= w&&mp[nx][ny]!='#') {
			 dfs(nx, ny, 0);
		 }
	 }
	 if (flag) {
		 cout << "Yes" << '\n';
	 }
	 else {
		 cout << "No" << '\n';
	 }
}

第二周任务 [Cloned] - Virtual Judge (vjudge.net)

思路: 这题起点是0,然后终点是1;然后每次都询问起点和终点的一半,如果是1,就继续查前半段,是0就查后半段;

#include<bits/stdc++.h>
using namespace std;
#define YES cout<<"YES"<<'\n';
#define NO  cout<<"NO"<<'\n';
#define endl cout<<'\n';
typedef long long ll;
const int maxn=1e6+10,inf = 1e18 ;
const int mod = 1e9 + 7;
using namespace std;
map<int, char>mp[maxn];
map<int, bool>vis[maxn];
int h, w;
int dir[4][2] = { {0,1},{1,0},{-1,0},{0,-1} };
int dx, dy;
bool flag = 0;
void dfs(int x,int y,int dep) {
	vis[x][y] = 1;
	if (x == dx && y == dy ) {
		flag = 1;
		return;
	}
	for (int i = 0; i < 4; i++) {
		int nx = x + dir[i][0];
		int ny = y + dir[i][1];
		if (!vis[nx][ny] && nx >= 1 && nx <= h && ny >= 1 && ny <= w&&mp[nx][ny]!='#') {
			if (nx == dx && ny == dy && dep == 0) continue;
			dfs(nx, ny, dep + 1);
		}
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin >> h >> w;
	 for (int i = 1; i <= h; i++) {
		 for (int j = 1; j <= w; j++) {
			 cin >> mp[i][j];
			 if (mp[i][j] == 'S') {
				 dx = i;
				 dy = j;
			 }
		 }
	 }
	 for (int i = 0; i < 4; i++) {
		 int nx = dx + dir[i][0];
		 int ny = dy + dir[i][1];
		 if (nx >= 1 && nx <= h && ny >= 1 && ny <= w&&mp[nx][ny]!='#') {
			 dfs(nx, ny, 0);
		 }
	 }
	 if (flag) {
		 cout << "Yes" << '\n';
	 }
	 else {
		 cout << "No" << '\n';
	 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值