总分
++++=
T725354 仙岛求药
思路
bfs模板,但是记得多组数据清空、换行
代码
#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(2e1 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
char c[N][N];
int sx, sy, ex, ey, n, m;
bool vis[N][N];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
struct Node{
int x, y, step;
};
void bfs(){
queue<Node> q;
q.push({sx, sy, 0});
vis[sx][sy] = 1;
while(q.size()){
Node f = q.front();
q.pop();
if(f.x == ex && f.y == ey){
cout << f.step << "\n";
return;
}
for(int i = 0; i < 4; i++){
int tx = f.x + dx[i];
int ty = f.y + dy[i];
if(tx > 0 && ty > 0 && tx <= n && ty <= m && c[tx][ty] != '#' && !vis[tx][ty]){
q.push({tx, ty, f.step + 1});
vis[tx][ty] = 1;
}
}
}
cout << "-1\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while(1){
cin >> n >> m;
if(n == 0 && m == 0){
break;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
if(c[i][j] == '@'){
sx = i;
sy = j;
}else if(c[i][j] == '*'){
ex = i;
ey = j;
}
}
}
bfs();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
vis[i][j] = 0;
}
}
}
return 0;
}P2873 [USACO07DEC] Mud Puddles S
思路
还是bfs模板,但是有负数,所以记得坐标都+=501(+=500也可以)
代码
#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e3 + 50)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
int ex, ey, mxx, mxy, n;
bool vis[N][N], c[N][N];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
struct Node{
int x, y, step;
};
void bfs(){
queue<Node> q;
q.push({501, 501, 0});
vis[501][501] = 1;
while(q.size()){
Node f = q.front();
q.pop();
if(f.x == ex && f.y == ey){
cout << f.step << "\n";
return;
}
for(int i = 0; i < 4; i++){
int tx = f.x + dx[i];
int ty = f.y + dy[i];
if(tx > 0 && ty > 0 && tx <= mxx + 10 && ty <= mxy + 10 && !c[tx][ty] && !vis[tx][ty]){
q.push({tx, ty, f.step + 1});
vis[tx][ty] = 1;
}
}
}
cout << "-1\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> ex >> ey >> n;
ex += 501;
ey += 501;
mxx = ex;
mxy = ey;
int x, y;
for(int i = 1; i <= n; i++){
cin >> x >> y;
x += 501;
y += 501;
c[x][y] = 1;
mxx = max(mxx, x);
mxy = max(mxy, y);
}
bfs();
return 0;
}T725348 走出迷宫的最短路径
思路
bfs+记录路径,记得”no way”
代码
#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(2e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
int sx, sy, ex, ey, n, m;
bool vis[N][N], c[N][N];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
struct Node{
int x, y;
string s;
};
void bfs(){
queue<Node> q;
q.push({sx, sy, "(" + to_string(sx) + "," + to_string(sy) + ")"});
vis[sx][sy] = 1;
while(q.size()){
Node f = q.front();
q.pop();
if(f.x == ex && f.y == ey){
cout << f.s;
return;
}
for(int i = 0; i < 4; i++){
int tx = f.x + dx[i];
int ty = f.y + dy[i];
if(tx > 0 && ty > 0 && tx <= n && ty <= m && !c[tx][ty] && !vis[tx][ty]){
q.push({tx, ty, f.s + "->(" + to_string(tx) + "," + to_string(ty) + ")"});
vis[tx][ty] = 1;
}
}
}
cout << "no way";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
}
}
cin >> sx >> sy >> ex >> ey;
bfs();
return 0;
}P2960 [USACO09OCT] Invasion of the Milkweed G
思路
乳草从最开始位置扩散,直到草地被填满输出次数即可,有坑:
- 先输入m,后输入n
- 先输入My,后输入Mx
- 坐标反过来了,所以Mx = n - Mx + 1
代码
#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
char c[N][N];
int sx, sy, n, m, mx;
bool vis[N][N];
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1};
struct Node{
int x, y, step;
};
void bfs(){
queue<Node> q;
q.push({sx, sy, 0});
vis[sx][sy] = 1;
while(q.size()){
Node f = q.front();
q.pop();
mx = max(mx, f.step);
for(int i = 0; i < 8; i++){
int tx = f.x + dx[i];
int ty = f.y + dy[i];
if(tx > 0 && ty > 0 && tx <= n && ty <= m && c[tx][ty] != '*' && !vis[tx][ty]){
q.push({tx, ty, f.step + 1});
vis[tx][ty] = 1;
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> m >> n >> sy >> sx;
sx = n - sx + 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
}
}
bfs();
cout << mx;
return 0;
}P10487 Nightmare II
思路
因为鬼能穿墙,所以先用曼波曼哈顿距离处理好每个坐标会在什么时候被鬼占领,然后bfs一编,算出女友会在什么时候走到哪些坐标,再bfs一遍小e算出答案
代码
#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(8e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
char c[N][N];
int sx, sy, ex, ey, n, m, zx1, zy1, zx2, zy2, mx, my, gx, gy, zt[N][N], gt[N][N], mt[N][N];
bool vis[N][N], viss[N][N];
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
struct Node{
int x, y, step;
};
struct Node1{
int x, y, step, val;
};
void init(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
zt[i][j] = (min(abs(i - zx1) + abs(j - zy1), abs(i - zx2) + abs(j - zy2)) + 1) / 2;
}
}
}
void bfsg(){
queue<Node> q;
q.push({gx, gy, 0});
vis[gx][gy] = 1;
gt[gx][gy] = 0;
while(q.size()){
Node f = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int tx = f.x + dx[i];
int ty = f.y + dy[i];
if(tx > 0 && ty > 0 && tx <= n && ty <= m && c[tx][ty] != 'X' && !vis[tx][ty] && zt[tx][ty] - 1 > f.step + 1){
q.push({tx, ty, f.step + 1});
vis[tx][ty] = 1;
gt[tx][ty] = f.step + 1;
}
}
}
}
void bfsm(){
queue<Node1> q;
q.push({mx, my, 0, 2});
viss[mx][my] = 1;
while(q.size()){
Node1 f = q.front();
q.pop();
if(gt[f.x][f.y] <= f.step){
cout << f.step << "\n";
return;
}
for(int i = 0; i < 4; i++){
int tx = f.x + dx[i];
int ty = f.y + dy[i];
if(tx > 0 && ty > 0 && tx <= n && ty <= m && c[tx][ty] != 'X' && !viss[tx][ty] && zt[tx][ty] > (f.val == 2 ? f.step + 1 : f.step)){
if(f.val == 2){
q.push({tx, ty, f.step + 1, 0});
mt[tx][ty] = f.step + 1;
}else{
q.push({tx, ty, f.step, f.val + 1});
mt[tx][ty] = f.step;
}
viss[tx][ty] = 1;
}
}
}
cout << "-1\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
gt[i][j] = 1e9;
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
if(c[i][j] == 'M'){
mx = i;
my = j;
}else if(c[i][j] == 'G'){
gx = i;
gy = j;
}else if(c[i][j] == 'Z'){
if(zx1){
zx2 = i;
zy2 = j;
}else{
zx1 = i;
zy1 = j;
}
}
}
}
init();
bfsg();
bfsm();
zx1 = 0;
zy1 = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
vis[i][j] = 0;
viss[i][j] = 0;
}
}
}
return 0;
}