Homework Introduction

#include <bits/stdc++.h>
using namespace std;
const int N = 25;
char mat[N][N];
int n,m,sx,sy,fx,fy,book[N][N];//book[i][j]从起点到ij点的距离
int nxt[4][2]={0,1,0,-1,1,0,-1,0};
struct node{
	int x,y,cost;
	friend bool operator < (node a,node b){
		return a.cost>b.cost;
	}
};
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mat[i][j];
			if(mat[i][j]=='Z')sx=i,sy=j;
			if(mat[i][j]=='W')fx=i,fy=j;
		}
	}
	memset(book,0x3f,sizeof(book));
	book[sx][sy] = 0;
	priority_queue<node>q;
	q.push({sx,sy,0});
	while(!q.empty()){
		node tmp=q.top();
		q.pop();
		for(int i=0;i<4;i++){
			int nx = tmp.x+nxt[i][0];
			int ny = tmp.y+nxt[i][1];
			if(nx>=1 && nx<=n && ny>=1 && ny<=m){
				if(mat[nx][ny]!='#'){
					int cost = tmp.cost+1;
					if(mat[nx][ny]>='1' && mat[nx][ny]<='9'){
						cost+=mat[nx][ny]-'0';
					}
					if(cost<book[nx][ny]){
						q.push({nx,ny,cost});
						book[nx][ny] = cost;
					}
				}
			}
		}
	}
	if(book[fx][fy]!=0x3f3f3f3f)cout<<book[fx][fy]<<endl;
	else cout<<"IMPOSSIBLE"<<endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
int mat[N][N],sx,sy,fx,fy;
int n,m,nxt[4][2]={0,1,0,-1,1,0,-1,0};
int book[N][N][10];
struct node{
	int x,y,hp,cost;
	friend bool operator <(node a,node b){
		return a.cost>b.cost;
	}
};
int main()
{
	int res = 1e9;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mat[i][j];
			if(mat[i][j]==2)sx=i,sy=j;
			if(mat[i][j]==3)fx=i,fy=j;
		}
	}
	memset(book,0x3f,sizeof(book));
	book[sx][sy][6] = 0;
	priority_queue<node>q;
	q.push({sx,sy,6,0});
	while(!q.empty()){
		node tmp = q.top();
		if(tmp.x==fx && tmp.y==fy){
			res = min(res,tmp.cost);
		}
		q.pop();
		for(int i=0;i<4;i++){
			int nx = tmp.x+nxt[i][0];
			int ny = tmp.y+nxt[i][1];
			if(nx>=1 && nx<=n && ny>=1 && ny<=m && mat[nx][ny]!=0 && tmp.hp>1){
				int hp=tmp.hp-1;
				int cost=tmp.cost+1;
				if(mat[nx][ny]==4)hp=6;
				if(cost<book[nx][ny][hp]){
					q.push({nx,ny,hp,cost});
					book[nx][ny][hp] = 0;
				}
			}
		}
	}
	if(res==1e9)res=-1;
	cout<<res<<endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 55;
int n,m,a[N][N],sx,sy,ex,ey,sd,dis[N][N][4];
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int Get(char x){
	if(x=='N') return 0;
	if(x=='S') return 2;
	if(x=='E') return 1;
	if(x=='W') return 3;
}
struct node{
	int x,y,dir,cost;
	friend bool operator <(node a,node b){
		return a.cost>b.cost;
	} 
};
priority_queue<node>q;
void bfs(){
	memset(dis,0x3f,sizeof(dis));
	q.push({sx,sy,sd,0});
	while(!q.empty()){
		node tmp = q.top();
		q.pop();
		int x = tmp.x;
		int y = tmp.y;
		int dir = tmp.dir;
		int Dir = (dir+1)%4;
		if(dis[x][y][Dir]>tmp.cost+1){
			dis[x][y][Dir] = tmp.cost+1;
			q.push({x,y,Dir,tmp.cost+1});
		}
		Dir = (dir+3)%4;
		if(dis[x][y][Dir]>tmp.cost+1){
			dis[x][y][Dir] = tmp.cost+1;
			q.push({x,y,Dir,tmp.cost+1});
		}
		int xx = x, yy = y;
		for(int j = 1; j<=3; j++){
			xx += dx[dir];
			yy += dy[dir];
			if(xx>=1 && xx+1<=n && yy>=1 && yy+1<=m){
				if(a[xx][yy]!=1 && a[xx][yy+1]!=1 && a[xx+1][yy]!=1 && a[xx+1][yy+1]!=1){
					if(dis[xx][yy][dir]>tmp.cost+1){
						dis[xx][yy][dir] = tmp.cost+1;
						q.push({xx,yy,dir,tmp.cost+1});
					}
				}else break;
			}else break;
		}
	}
	int ans = 0x3f3f3f3f;
	for(int i = 0; i<4; i++){
		ans = min(ans,dis[ex][ey][i]);
	}
	if(ans==0x3f3f3f3f) cout<<-1<<endl;
	else cout<<ans<<endl;
}
int main(){
	cin>>n>>m;
	for(int i = 1; i<=n; i++){
		for(int j = 1; j<=m; j++){
			cin>>a[i][j];
		}
	}
	cin>>sx>>sy>>ex>>ey;
	char x;
	cin>>x;
	sd = Get(x);
	if(sx==ex && sy==ey){
		cout<<0<<endl;
		return 0;
	}
	bfs();
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n,m,d,r;
char mat[N][N];
int book[N][N][2];
int nxt[4][2]={0,1,0,-1,1,0,-1,0};
struct node{
	int x,y,cost,flag;
};
int main()
{
	cin>>n>>m>>d>>r;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mat[i][j];
		}
	}
	memset(book,0x3f,sizeof(book));
	book[1][1][0] = 0;
	queue<node>q;
	q.push({1,1,0,0});
	while(!q.empty()){
		node tmp = q.front();
		q.pop();
		for(int i=0;i<4;i++){
			int nx = tmp.x+nxt[i][0];
			int ny = tmp.y+nxt[i][1];
			int cost = tmp.cost+1;
			int flag = tmp.flag;
			if(nx>=1 && nx<=n && ny>=1 && ny<=m){
				if(mat[nx][ny]!='#'){
					if(cost<book[nx][ny][flag]){
						book[nx][ny][flag] = cost;
						q.push({nx,ny,cost,flag});
					}
				}
			}
		}
		if(tmp.flag==0){
			int nx = tmp.x+d;
			int ny = tmp.y+r;
			int cost = tmp.cost+1;
			if(nx>=1 && nx<=n && ny>=1 && ny<=m && mat[nx][ny]!='#'){
				if(cost<book[nx][ny][1]){
					book[nx][ny][1] = cost;
					q.push({nx,ny,cost,1});
				}
			}
		}
	}
	int res = min(book[n][m][0],book[n][m][1]);
	if(res==0x3f3f3f3f)res = -1;
	cout<<res<<endl;
	return 0;
}

Problem

Please claim the assignment to see the problems.
Status
Live...
Problem
25
Open Since
2025-11-5 0:00
Deadline
2026-1-31 23:59
Extension
24 hour(s)