A:
點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
const int maxx=1005;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
int n,sx,sy,tx,ty;
char a[maxx][maxx];
int d[maxx][maxx];
bool vis[maxx][maxx];
bool exist(int x,int y){
return 1<=x && 1<=y && x<=n && y<=n;
}
void bfs(){
memset(vis,false,sizeof(vis));
queue<pair <int,int> >q;
q.push(make_pair(sx,sy));
d[sx][sy]=0;
vis[sx][sy]=true;
while(q.size()){
pair<int,int> u=q.front();
q.pop();
int x=u.first,y=u.second;
if(x==tx&&y==ty){
cout<<d[tx][ty]<<endl;
return;
}
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(exist(nx,ny)&&a[nx][ny]=='0'&&!vis[nx][ny]){
d[nx][ny]=d[x][y]+1;
vis[nx][ny]=true;
q.push(make_pair(nx,ny));
}
}
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
cin>>a[i]+1;
}
cin>>sx>>sy>>tx>>ty;
bfs();
return 0;
}
B:
點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
const int N = 1005;
int n;
bool hica[N][N];
int a[N][N];
int dx[8] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int ans1, ans2;
bool fw(int x, int y) {
return x > 0 && x <= n && y > 0 && y <= n;
}
void bfs(int sx, int sy) {
queue<pair<int, int> >q;
q.push(make_pair(sx, sy));
int val = a[sx][sy];
hica[sx][sy]=true;
bool f1 = false, f2 = false;
while (q.size()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 8; i++) {
if (fw(x + dx[i], y + dy[i])) {
if (a[x + dx[i]][y + dy[i]] != val) {
if (a[x + dx[i]][y + dy[i]] > val)f2 = true;
if (a[x + dx[i]][y + dy[i]] < val)f1 = true;
} else if (!hica[x + dx[i]][y + dy[i]]) {
hica[x + dx[i]][y + dy[i]] = true;
q.push(make_pair(x + dx[i], y + dy[i]));
}
}
else continue;
}
}
if(!f1&&!f2)ans1++;
if(f1&&!f2)ans1++;
if(!f1&&f2)ans2++;
return;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(!hica[i][j]){
bfs(i,j);
}
}
}
cout<<ans1<<" "<<ans2<<endl;
return 0;
}
C:
點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
const int N=360,M=16;
int n,m,c1,c2,dist;
int sx,sy,tx,ty;
int cov[N][N];
bool person[N][N];
struct data{
int x,y,u1,u2;
data(int v1,int v2,int v3,int v4) : x(v1),y(v2),u1(v3),u2(v4){}
};
int d[N][N][M][M],vis[N][N][M][M];
const int dx[8]={-1,0,1,0,-1,1,1,-1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
inline bool exist(int x,int y){
return 1<=x && x<=n && 1<=y && y<=m;
}
void bfs(){
queue<data> q;
d[sx][sy][0][0]=0;
vis[sx][sy][0][0]=1;
q.push((data){sx,sy,0,0});
while(q.size()){
data u=q.front();
q.pop();
for(int i=0;i<8;i++){
int nx=u.x+dx[i];
int ny=u.y+dy[i];
if(exist(nx,ny) && !person[nx][ny]){
if(cov[nx][ny]){
if(u.u1+1<=c1 && !vis[nx][ny][u.u1+1][u.u2]){
d[nx][ny][u.u1+1][u.u2]=d[u.x][u.y][u.u1][u.u2]+1;
vis[nx][ny][u.u1+1][u.u2]=true;
q.push((data){nx,ny,u.u1+1,u.u2});
}
}
else{
if(!vis[nx][ny][u.u1][u.u2]){
d[nx][ny][u.u1][u.u2]=d[u.x][u.y][u.u1][u.u2]+1;
vis[nx][ny][u.u1][u.u2]=true;
q.push((data){nx,ny,u.u1,u.u2});
}
}
}
}
if(u.u2+1<=c2){
for(int i=0;i<4;i++){
int nx=u.x+dx[i]*dist,ny=u.y+dy[i]*dist;
if(exist(nx,ny) && !person[nx][ny]){
if(cov[nx][ny]){
if(u.u1+1<=c1 && !vis[nx][ny][u.u1+1][u.u2+1]){
d[nx][ny][u.u1+1][u.u2+1]=d[u.x][u.y][u.u1][u.u2]+1;
vis[nx][ny][u.u1+1][u.u2+1]=1;
q.push((data){nx,ny,u.u1+1,u.u2+1});
}
}
else {
if(!vis[nx][ny][u.u1][u.u2+1]){
d[nx][ny][u.u1][u.u2+1]=d[u.x][u.y][u.u1][u.u2]+1;
vis[nx][ny][u.u1][u.u2+1]=1;
q.push((data){nx,ny,u.u1,u.u2+1});
}
}
}
}
}
}
return ;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>c1>>c2>>dist;
char s[4];
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>s;
if(s[0]=='S') sx=i,sy=j;
else if(s[0]=='T') tx=i,ty=j;
else if(s[0]!='.'){
person[i][j]=true;
int len=strlen(s),num=0;
for(int k=0;k<len;k++){
num=num*10+s[k]-'0';
}
for(int t=max(1,i-num+1);t<=min(n,i+num-1);t++){
int l=max(1,j-num+1+abs(i-t)),
r=min(m,j+num-1-abs(i-t));
cov[t][l]++,cov[t][r+1]--;
}
}
}
}
for(int i=1;i<=n;i++){
for(int j=2;j<=m;j++){
cov[i][j]+=cov[i][j-1];
}
}
bfs();
int t=n*m,u1,u2;
for(int i=0;i<=c1+c2;i++){
for(int j=0;j<=i;j++){
if(vis[tx][ty][j][i] && d[tx][ty][j][i]<t){
t=d[tx][ty][j][i];
u1=j;
u2=i-j;
}
}
}
if(u2==2) u2++;
if(t==n*m) cout<<"-1\n";
else cout<<t<<" "<<u1<<" "<<u2<<"\n";
return 0;
}
D:
點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e2 + 10;
int n,m;
bool vis[N][N];
int dist[N][N];
//dx和dy是點之間的轉移
int dx[] = {-1,-1,1,1};
int dy[] = {-1,1,1,-1};
int gx[] = {-1,-1,0,0};
int gy[] = {-1,0,0,-1};
char g[] = "\\/\\/"; // '\' '/' '\' '/'
typedef pair<int,int> pii;
char G[N][N];
void bfs(){
deque<pii> dq;
dist[1][1] = 0;
dq.push_front({1,1});
while(!dq.empty()){
auto u = dq.front();
dq.pop_front();
int x = u.first,y = u.second;
if(x == n+1 && y == m+1) return;
if(vis[x][y]) continue;
vis[x][y] = 1;
for(int i=0,xx,yy,gxx,gyy,w;i<4;++i){
xx = x + dx[i],yy = y + dy[i];
if(xx > n+1 || xx < 1 || yy > m+1 || yy < 1) continue;
gxx = x + gx[i],gyy = y + gy[i];
w = (G[gxx][gyy] != g[i]);
if(dist[x][y] + w < dist[xx][yy]){
dist[xx][yy] = dist[x][y] + w;
if(w == 0) dq.push_front({xx,yy});
else dq.push_back({xx,yy});
}
}
}
}
void solve(){
memset(dist,0x3f,sizeof dist);
memset(vis,0,sizeof vis);
cin >> n >> m;
for(int i=1;i<=n;++i) cin >> (G[i] + 1);
if(n + m & 1){
cout << "NO SOLUTION\n";
return;
}
bfs();
cout << dist[n+1][m+1] << '\n';
}
int main(){
std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int t;
cin >> t;
while(t--)
solve();
return 0;
}
E:
點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
const int dx[5]= {0,1,0,-1,0};
const int dy[5]= {0,0,-1,0,1};
bool ok=0;
int T,n,m,a[810][810],kj[3][3],f1[1000000][3],f2[1000000][3],v1[810][810],v2[810][810];
char c;
bool check1(int x,int y,int s) {
if(x>0&&y>0&&x<=n&&y<=m&&a[x][y]!=1&&a[x][y]!=-1) {
int lyx1=abs(kj[1][1]-x)+abs(kj[1][2]-y),lyx2=abs(kj[2][1]-x)+abs(kj[2][2]-y);
if(lyx1>s*2&&lyx2>s*2)
return 1;
else
return 0;
} else return 0;
}
bool check2(int x,int y,int s) {
if(x>0&&y>0&&x<=n&&y<=m&&a[x][y]!=1&&a[x][y]!=-1) {
int lyx1=abs(kj[1][1]-x)+abs(kj[1][2]-y),lyx2=abs(kj[2][1]-x)+abs(kj[2][2]-y);
if(lyx1>s*2&&lyx2>s*2)
return 1;
else
return 0;
} else return 0;
}
inline int read()
{
int x=0,f=1;
char s=getchar();
while(s<'0'||s>'9')
{
if(s=='-') f=-f;
s=getchar();
}
while(s>='0'&&s<='9')
x=x*10+s-'0',s=getchar();
return x*f;
}
void bfs() {
int h1=0,t1=1,h2=0,t2=1,s1=0,e1=1,s2=0,e2=1;
for(int s=1; h1!=t1||h2!=t2; s++) {
for(int i=1; i<=3; i++) {
for(int j=abs(s1-e1); j>0; j--) {
h1=(h1%999999)+1,s1++;
for(int ii=1; ii<=4; ii++) {
int xx=f1[h1][1]+dx[ii],yy=f1[h1][2]+dy[ii];
if(check1(xx,yy,s)&&!v1[xx][yy]&&check1(f1[h1][1],f1[h1][2],s)) {
t1=(t1%999999)+1,e1++;
f1[t1][1]=xx,f1[t1][2]=yy;
v1[xx][yy]=1;
if(v2[xx][yy]==1) {
printf("%d\n",s);
return ;
}
}
}
}
}
for(int j=abs(s2-e2); j>0; j--) {
h2=(h2%999999)+1,s2++;
for(int ii=1; ii<=4; ii++) {
int xx=f2[h2][1]+dx[ii],yy=f2[h2][2]+dy[ii];
if(check2(xx,yy,s)&&!v2[xx][yy]&&check2(f2[h2][1],f2[h2][2],s)) {
t2=(t2%999999)+1,e2++;
f2[t2][1]=xx,f2[t2][2]=yy;
v2[xx][yy]=1;
if(v1[xx][yy]==1) {
printf("%d\n",s);
return ;
}
}
}
}
}
cout<<-1<<endl;
return;
}
int main() {
T=read();
while(T--) {
memset(v1,0,sizeof(v1));
memset(v2,0,sizeof(v2));
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
ok=0;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
cin>>c;
if(c=='X') a[i][j]=1;
if(c=='Z'&&!ok)
kj[1][1]=i,kj[1][2]=j,a[i][j]=-1,ok=1;
if(c=='Z'&&ok==1)
kj[2][1]=i,kj[2][2]=j,a[i][j]=-1;
if(c=='M')
f1[1][1]=i,f1[1][2]=j,v1[i][j]=1;
if(c=='G')
f2[1][1]=i,f2[1][2]=j,v2[i][j]=1;
}
}
bfs();
}
}
F:
點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
inline void relax(int &a,const int &b){
if(b>a) a=b;
}
struct point{
int x,y;
point(){}
point(const int &_x,const int &_y):x(_x),y(_y){}
};
const int N=1001;
const int ndir=4;
const int dx[ndir]={-1,0,0,1};
const int dy[ndir]={0,1,-1,0};
int n,m;
int p[N][N];
bool valid[N][N];
int q_n;
point q[N*N];
inline bool check(const int &mid){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
valid[i][j]=p[i][j]<=mid;
}
}
valid[1][1]=false;
q_n=0,q[q_n++] =point(1,1);
for(int i=0;i<q_n;i++){
point u=q[i];
for(int d=0;d<ndir;d++){
int x=u.x+dx[d];
int y=u.y+dy[d];
if(x>0 && y>0 && y<=m && valid[x][y]){
valid[x][y]=false;
if(x==n) return true;
q[q_n++]=point(x,y);
}
}
}
return false;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
int l=1,r=0,ans=N;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>p[i][j];
relax(r,p[i][j]);
}
}
while(l<=r){
int mid=l+r>>1;
if(check(mid)) r=mid-1,ans=mid;
else l=mid+1;
}
cout<<ans<<"\n";
return 0;
}
G:
點選檢視程式碼
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N=1600;
int n,m;
bool vis[N][N],useful[N][N];
char g[N][N];
int dis[N][N];
int x,y,x1,y2;//獵物,巨龍
int ans;
PII q[N*N];
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
void make_useful()
{
// (x,y)
useful[x][y]=true;
bool flag;
/* for(int i=1;i<x;i++) //同一列
{
if(g[i][y]=='X') flag=true;
if(flag) useful[i][y]=false;
else useful[i][y]=true;
}
flag=false;
for(int i=x+1;i<=n;i++)
{
if(g[i][y]=='X') flag=true;
if(flag) useful[i][y]=false;
else useful[i][y]=true;
}
flag=false;
for(int i=1;i<y;i++) //同一行
{
if(g[x][i]=='X') flag=true;
if(flag) useful[x][i]=false;
else useful[x][i]=true;
}
flag=false;
for(int i=y+1;i<=m;i++)
{
if(g[x][i]=='X') flag=true;
if(flag) useful[x][i]=false;
else useful[x][i]=true;
}
//flag=false;*/
for(int i=0;i<8;i++)
{
flag=false;
int nx=x+dx[i],ny=y+dy[i];
while(nx>0&&nx<=n&&ny>0&&ny<=m)
{
if(g[nx][ny]=='X') flag=true;
if(flag) useful[nx][ny]=false;
else useful[nx][ny]=true;
nx+=dx[i];ny+=dy[i];
}
}
}
bool bfs()
{
memset(vis,0,sizeof(vis));
int hh=0,tt=0;
q[0]=make_pair(x1,y2);
dis[x1][y2]=0;
vis[x1][y2]=1;
while(hh<=tt)
{
PII tmp=q[hh++];
if(useful[tmp.x][tmp.y])
{
ans=dis[tmp.x][tmp.y];
return true;
}
for(int i=0;i<4;i++)
{
int nx=tmp.x+dx[i],ny=tmp.y+dy[i];
if(g[nx][ny]=='X') continue;
if(nx<1||nx>n||ny<1||ny>m) continue;
if(vis[nx][ny]) continue;
dis[nx][ny]=dis[tmp.x][tmp.y]+1;
vis[nx][ny]=1;
q[++tt]=make_pair(nx,ny);
}
}
return false;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",g[i]+1);
while(~scanf("%d%d%d%d",&x,&y,&x1,&y2)&&x&&y&&x1&&y2)
{
memset(useful,0,sizeof(useful));
make_useful();
if(bfs()) printf("%d\n",ans);
else puts("Impossible!");
}
return 0;
}
H:
點選檢視程式碼
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N=105;
char g[N][N];
int n;
int dis[N][N];
bool vis[N][N];
PII q[N*N];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int st;
int ans;
int sx,sy,tx,ty;
bool bfs()
{
int times=0;
int hh=0,tt=0;
q[0]=make_pair(sx,sy);
vis[sx][sy]=1;dis[sx][sy]=0;
while(hh<=tt)
{
//times++;
int S=tt-hh+1;
for(int j=1;j<=S;j++)
{
PII tmp=q[hh++];
for(int i=0;i<4;i++)
{
int nx=tmp.x+dx[i],ny=tmp.y+dy[i];
if(nx<0||nx>=n||ny<0||ny>=n) continue;
if(vis[nx][ny]) continue;
if(g[nx][ny]=='*') continue;
dis[nx][ny]=dis[tmp.x][tmp.y]+1;
//cout<<"人"<<' '<<nx<<' '<<ny<<' '<<dis[nx][ny]<<endl;
//if(nx==tx&&ny==ty)
// {
// ans=dis[nx][ny];
// return true;
// }
vis[nx][ny]=1;
q[++tt]=make_pair(nx,ny);
}
}
if(vis[tx][ty])
{
printf("%d\n",times);
return true;
}
//times++;
//cout<<"狗"<<' '<<tx<<' '<<ty<<endl;
int nx=tx+dx[st],ny=ty+dy[st];
int cnt=0;
while((g[nx][ny]=='*'||nx<0||nx>=n||ny<0||ny>=n)&&cnt<=3)
{
st=(st+1)%4;
nx=tx+dx[st];ny=ty+dy[st];
cnt++;
if(cnt>3) return false;
}
tx=nx;ty=ny;
//if(vis[tx][ty])
// {
// printf("%d\n",times);
// return true;
//}
times++;
}
return false;
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++) scanf("%s",g[i]);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(g[i][j]=='F') sx=i,sy=j;
else if(g[i][j]=='J') tx=i,ty=j;
}
//cout<<sx<<' '<<sy<<' '<<tx<<' '<<ty<<endl;
if(!bfs()) puts("No solution.");
return 0;
}
I:
點選檢視程式碼
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 1e3 + 5;
const int dx[] = { 0 , 1 , -1 , 0 , 0 , 1 , 1 , -1 , -1 };
const int dy[] = { 0 , 0 , 0 , 1 , -1 , 1 , -1 , 1 , -1 };
int read ()
{
int x = 0 , f = 1;
char ch = cin.get();
while ( !isdigit ( ch ) ) { if ( ch == '-' ) f = -1; ch = cin.get(); }
while ( isdigit ( ch ) ) { x = ( x << 1 ) + ( x << 3 ) + ( ch ^ 48 ); ch = cin.get(); }
return x * f;
}
int n , m , mp[N][N] , head = 1 , tail = 0 , color , col[N][N];
struct node { int x , y; } q[100000000];
int check ( int x , int y ) { return 0 <= x && x <= n && 0 <= y && y <= n && !col[x][y]; }
void dfs ( int x , int y )
{
for ( int i = 1 ; i <= 8 ; i ++ )
{
int tx = x + dx[i] , ty = y + dy[i];
if ( !check ( tx , ty ) ) continue;
if ( mp[tx][ty] == mp[x][y] )
{
col[tx][ty] = col[x][y];
dfs ( tx , ty );
}
else
{
col[tx][ty] = col[x][y] + 1;
q[++tail] = (node) { tx , ty };
}
}
}
void bfs ()
{
head = 1 , tail = 0;
q[++tail] = (node) { 0 , 0 };
col[0][0] = 1;
while ( head <= tail )
{
node u = q[head++];
int x = u.x , y = u.y;
dfs ( x , y );
}
}
char ch;
signed main ()
{
ios::sync_with_stdio(false);
cin.tie(0) , cout.tie(0);
n = read() , m = read();
for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) cin >> ch , mp[i][j] = ch - '0';
n += 1;
bfs();
while ( m -- )
{
int i = read() , j = read();
cout << ( col[i][j] - 1 ) / 2 << ' ';
}
return 0;
}
J:
點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
const int N=510;
int n,m;
char s[N][N];
struct node{
int x,y,t;
};
int sx,sy,st;
int tx,ty,tt;
void pre(){
for(int i=n;i>=1;i--){
for(int j=m;j>=1;j--){
if(s[i][j]=='X'){
sx=i,sy=j,st=0;
if(s[i][j-1]=='X')st=1;
if(s[i-1][j]=='X')st=2;
return ;
}
}
}
}
void prepare(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(s[i][j]=='O'){
tx=i,ty=j,tt=0;
return ;
}
}
}
}
bool exist(node sta){
int x=sta.x,y=sta.y,t=sta.t;
if(x<1 || x>n || y<1 || y>m || s[x][y]=='#') return false;
if(t==0) return s[x][y]!='E';
if(t==1) return 1<y && s[x][y-1]!='#';
else return 1<x && s[x-1][y]!='#';
}
int dx[3][4]={
{-1,0,2,0},
{-1,0,1,0},
{-2,0,1,0}
};
int dy[3][4]={
{0,2,0,-1},
{0,1,0,-2},
{0,1,0,-1}
};
int dt[3][4]={
{2,1,2,1},
{1,0,1,0},
{0,2,0,2}
};
int d[N][N][3];
bool vis[N][N][3];
void bfs(){
memset(d,0,sizeof(d));
memset(vis,false,sizeof(vis));
queue<node> q;
q.push((node){sx,sy,st});
d[sx][sy][st]=0;
vis[sx][sy][st]=1;
while(q.size()){
node u=q.front();
q.pop();
int x=u.x,y=u.y,t=u.t;
if(x==tx && y==ty && t==tt){
cout<<d[x][y][t]<<"\n";
// for(int i=1;i<=n;i++){
// for(int j=1;j<=m;j++){
// cout<<d[i][j][t]<<" ";
// }cout<<"\n";
// }
return ;
}
for(int i=0;i<4;i++){
node v=(node){x+dx[t][i],y+dy[t][i],dt[t][i]};
if(exist(v) && !vis[v.x][v.y][v.t]){
d[v.x][v.y][v.t]=d[x][y][t]+1;
vis[v.x][v.y][v.t]=1;
q.push(v);
}
}
}
cout<<"Impossible\n";
}
void work(){
for(int i=1;i<=n;i++){
cin>>s[i]+1;
}
pre();
prepare();
bfs();
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while(cin>>n>>m,n&&m) work();
return 0;
}
//抄襲絕非良策,理解才是正道