第二周任务 [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';
}
}