A
和之前的17号的题没区别,不过是多组输入
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
char a[25][25];
bool st[25][25];
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int n,m,sx,sy,ex,ey;
struct node
{
int x,y;
};
int minn=2e6;
void bfs(int sx,int sy)
{
minn=1;
queue<node>q;
node now,ne;
now.x=sx,now.y=sy;
st[sx][sy]=1;
q.push(now);
while(!q.empty())
{
node t=q.front();
q.pop();
for(int i=0;i<4;i++)
{
ne.x=t.x+dx[i],ne.y=t.y+dy[i];
if(ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<m&&a[ne.x][ne.y]=='.'&&!st[ne.x][ne.y])
{
st[ne.x][ne.y]=true;
minn++;
q.push(ne);
}
}
}
}
int main()
{
while(cin>>m>>n)
{
memset(st,0,sizeof st);
if(n==0&&m==0)break;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
cin>>a[i][j];
if(a[i][j]=='@')
sx=i,sy=j;
}
minn=0;
bfs(sx,sy);
cout<<minn<<endl;
}
return 0;
}
B
和17号题一样,过了
C
先写牛客,没空写这题,直接放题解复习
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
//#define int long long
using namespace std;
const int INF=0x3f3f3f3f;
const int manx=210;
int mp[manx][manx],times[manx][manx],numk[manx][manx];
int n,m,k,x1,yy1,x2,y2,flag,mmax;
char str[manx];
int dx[]= {0,-1,1,0};
int dy[]= {-1,0,0,1};
struct node
{
int x,y,step,k;/*
friend bool operator<(node a,node b)
{
if(a.step==b.step)
return a.k<b.k;
return a.step>b.step;
}*/
};
int check(int x,int y,int k)
{
if(x<0||x>=n||y<0||y>=m)
return 0;
if(k<0)
return 0;
return 1;
}
void bfs()
{
if(x1==x2&&yy1==y2)
{
flag=1;
mmax=0;
return;
}
queue<node>qu;
numk[x1][yy1]=k;
qu.push(node{x1,yy1,0,k});
while(!qu.empty())
{
if(flag)
break;
node now=qu.front();
qu.pop();
for(int i=0; i<4; i++)
{
node net;
net.x=now.x+dx[i];
net.y=now.y+dy[i];
net.step=now.step+1;
net.k=now.k-mp[net.x][net.y];
if(check(net.x,net.y,net.k))
{
if(numk[net.x][net.y]>=net.k)
continue;
numk[net.x][net.y]=net.k;
if(net.x==x2&&net.y==y2)
{
flag=1;
mmax=net.step;
break;
}
qu.push(net);
}
}
}
}
void init()
{
//memset(times,INF,sizeof(times));
memset(numk,-1,sizeof(numk));
memset(mp,0,sizeof(mp));
flag=0;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
init();
for(int i=0; i<n; i++)
{
scanf("%s",str);
for(int j=0; j<m; j++)
{
if(str[j]=='@')
x1=i,yy1=j;
else if(str[j]=='+')
x2=i,y2=j;
else if(str[j]=='#')
mp[i][j]=1;
}
}
bfs();
if(flag)
printf("%d\n",mmax);
else
printf("-1\n");
return 0;
}