A:直接dfs
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
#define ll long long int
const int maxn=10;
char Map[maxn][maxn],temp[maxn];
bool col[maxn],row[maxn];
ll ans;
int N,k;
void dfs(int n,int x,int y)
{
if(n==k){
ans++;
return;
}
int nx,ny,flag=0,isfirst=1;
for(int i=x;i<=N;i++){
for(int j=(isfirst?(y+1):1);j<=N;j++){
if('#'==Map[i][j]&&!row[i]&&!col[j]){
nx=i,ny=j;
flag=1;
break;
}
}
isfirst=0;
if(flag) break;
}
if(flag){
row[nx]=col[ny]=true;
dfs(n+1,nx,ny);
row[nx]=col[ny]=false;
dfs(n,nx,ny);
}
}
int main()
{
while(scanf("%d%d",&N,&k)&&(N!=-1||k!=-1)){
for(int i=1;i<=N;i++){
scanf("%s",temp);
sprintf(Map[i]," %s",temp);
}
ans=0;
memset(col,false,sizeof(col));
memset(row,false,sizeof(row));
dfs(0,1,0);
printf("%lld\n",ans);
}
return 0;
}
B:朝上下东西南北四个方向bfs
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define ll long long int
#define res regiter int
#define inf 0x3f3f3f3f
int H,N,M,bh,bx,by,eh,ex,ey;
const int maxn=35;
char Map[maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int to[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct Node{
int h,x,y,step;
};
bool isgo(int h,int x,int y){
if(vis[h][x][y]) return false;
if(h<1||h>H||x<1||x>N||y<1||y>M) return false;
if('#'==Map[h][x][y]) return false;
return true;
}
int bfs()
{
queue<Node> q;
while(!q.empty()) q.pop();
q.push((Node){bh,bx,by,0});
int step=0;
while(!q.empty())
{
Node now=q.front();
q.pop();
for(int i=0;i<6;i++){
int nh=now.h+to[i][0],nx=now.x+to[i][1],ny=now.y+to[i][2];
if(isgo(nh,nx,ny)){
if(nh==eh&&nx==ex&&ny==ey){
step=now.step+1;
break;
}
vis[nh][nx][ny]=true;
q.push((Node){nh,nx,ny,now.step+1});
}
}
if(step) break;
}
return step;
}
void init()
{
memset(vis,false,sizeof(vis));
vis[bh][bx][by]=true;
}
int main()
{
while(scanf("%d%d%d",&H,&N,&M)&&(H||N||M)){
char temp[maxn];
for(int i=1;i<=H;i++){
for(int j=1;j<=N;j++){
scanf("%s",temp);
sprintf(Map[i][j],"#%s",temp);
}
}
for(int i=1;i<=H;i++){
for(int j=1;j<=N;j++){
for(int k=1;k<=M;k++){
if('S'==Map[i][j][k]) bh=i,bx=j,by=k;
if('E'==Map[i][j][k]) eh=i,ex=j,ey=k;
}
}
}
init();
int result=bfs();
if(result) printf("Escaped in %d minute(s).\n",result);
else printf("Trapped!\n");
}
return 0;
}
C :
这个题还犹豫了一下,之前做过一个,那个是怎样由0最快地到达N,这种情况就反着对N进行操作,N为偶数,N/=2,N为奇数,N-=1;
对于这个题来说,bfs无疑是很好的选择。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define N 100010
int step[N],vis[N];
queue<int>q;
int bfs(int n,int k){
int now,next;
step[n]=0;
vis[n]=1;
q.push(n);
while(!q.empty()){
now=q.front();
q.pop();
for(int i=0;i<3;i++){
if(i==0) next=now-1;
else if(i==1) next=now+1;
else if(i==2) next=now*2;
if(next<0||next>N) continue;
if(!vis[next]){
vis[next]=1;
q.push(next);
step[next]=step[now]+1;
}
if(next==k) return step[next];
}
}
}
int main(){
int n,k;
scanf("%d%d",&n,&k);
if(n>=k) printf("%d\n",n-k);
else printf("%d\n",bfs(n,k));
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
#define ll long long int
#define res regiter int
#define inf 0x3f3f3f3f
const int maxn=100010;
int step[maxn];
int l,r,a[maxn*2],N,K;
int main()
{
scanf("%d%d",&N,&K);
if(N>=K){
printf("%d",N-K);
return 0;
}
a[r++]=N;
while(l<r){
int pre=a[l++];
if(pre==K){
printf("%d",step[pre]);
return 0;
}
if(pre+1<=maxn&&!step[pre+1]) step[pre+1]=step[pre]+1,a[r++]=pre+1;
if(0<=pre-1&&!step[pre-1]) step[pre-1]=step[pre]+1,a[r++]=pre-1;
if(pre*2<=maxn&&!step[pre*2]) step[pre*2]=step[pre]+1,a[r++]=pre*2;
}
return 0;
}
D:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int maxn = 20;
const int INF = 1e9;
int n,m,T,ansv,a[maxn][maxn],ans[maxn][maxn],b[maxn][maxn];
void solve() {
int cur[maxn][maxn];
memcpy(cur,a,sizeof(a));
for(int i=1;i<=m;i++)
if(b[1][i]) {
cur[1][i] ^= 1;
if(2 <= n) cur[2][i] ^= 1;
if(i > 1) cur[1][i-1] ^= 1;
if(i < m) cur[1][i+1] ^= 1;
}
for(int i=2;i<=n;i++)
for(int j=1;j<=m;j++)
if(cur[i-1][j]) {
b[i][j] = 1;
cur[i-1][j] = 0;
cur[i][j] ^= 1;
if(i < n) cur[i+1][j] ^= 1;
if(j > 1) cur[i][j-1] ^= 1;
if(j < m) cur[i][j+1] ^= 1;
}
bool ok = true;
for(int i=1;i<=m;i++)
if(cur[n][i]) { ok = false; break; }
if(ok) {
int c = 0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(b[i][j]) c++;
if(c < ansv) {
memcpy(ans,b,sizeof(b));
ansv = c;
}
else if(c == ansv) {
bool flage = true;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(ans[i][j] > b[i][j]) break;
else if(ans[i][j] < b[i][j]) { flage = false; break; }
if(flage) memcpy(ans,b,sizeof(b));
}
}
}
int main() {
while(~scanf("%d%d",&n,&m)) {
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
ansv = INF;
for(int i=0;i<(1<<m);i++) {
memset(b,0,sizeof(b));
for(int j=0;j<m;j++)
if(i & (1<<j)) b[1][j+1] = 1;
else b[1][j+1] = 0;
solve();
}
if(ansv == INF) printf("IMPOSSIBLE\n");
else for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(j == 1) printf("%d",ans[i][j]);
else printf(" %d",ans[i][j]);
}
printf("\n");
}
}
return 0;
}
E
垃圾题目,坑了我好久。结果作为参数返回main函数就wa???直接printf就AC???
#include <iostream>
#include <cstring>
#include <queue>
#include <cstdio>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
struct Node{
ll x,now;
};
ll bfs(int n)
{
queue<Node> q;
if(1==n%2) q.push((Node){1,1});
q.push((Node){0,1});
while(!q.empty()){
Node temp=q.front();
q.pop();
if(0==temp.x%n&&temp.x){
return temp.x;
}
q.push((Node){temp.x,temp.now*10});
q.push((Node){temp.x+temp.now*10,temp.now*10});
}
}
int main()
{
int N;
while(scanf("%d",&N)&&N){
printf("%lld\n",bfs(N));
}
return 0;
}
F:直接打表筛素数。因为步数是非常有限的, 所以bfs是最好的选择,改变每一位上的数字,另加vis数组判重。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
const int maxn=1e5+10;
int N,x1,x2;
bool notprime[maxn],vis[maxn];
struct Node{
int x,step;
};
void bfs(int n){
queue<Node> q;
q.push((Node){x1,0});
vis[x1]=true;
while(!q.empty()){
Node now=q.front();
q.pop();
if(x2==now.x){
printf("%d\n",now.step);
return;
}
for(int i=1;i<=9;i++){
int temp=now.x%1000+i*1000;
if(!vis[temp]&&!notprime[temp]){
q.push((Node){temp,now.step+1});
vis[temp]=true;
}
}
for(int i=0;i<=9;i++){
int temp=now.x%100+i*100+now.x/1000*1000;
if(!vis[temp]&&!notprime[temp]){
q.push((Node){temp,now.step+1});
vis[temp]=true;
}
}
for(int i=0;i<=9;i++){
int temp=now.x/100*100+i*10+now.x%10;
if(!vis[temp]&&!notprime[temp]){
q.push((Node){temp,now.step+1});
vis[temp]=true;
}
}
for(int i=0;i<=9;i++){
int temp=now.x/10*10+i;
if(!vis[temp]&&!notprime[temp]){
q.push((Node){temp,now.step+1});
vis[temp]=true;
}
}
}
printf("Impossible\n");
}
void init()
{
for(int i=2;i<=maxn;i++){
if(!notprime[i]){
for(int j=2;j*i<maxn;j++)
notprime[j*i]=true;
}
}
}
int main()
{
init();
scanf("%d",&N);
for(int i=0;i<N;i++){
memset(vis,false,sizeof(vis));
scanf("%d %d",&x1,&x2);
bfs(x1);
}
return 0;
}
G:好好学英语
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
const int maxn=205;
char str1[maxn],str2[maxn],str[maxn];
void slove()
{
int step=1,len=strlen(str1);
char temp1[maxn],temp2[maxn],temp[maxn];
for(int i=0,cnt=0;i<len;i++){
temp[cnt++]=str2[i];
temp[cnt++]=str1[i];
}
temp[len*2]='\0';
while(1)
{
if(0==strcmp(temp,str)){
printf("%d\n",step);
return;
}else if(0==strcmp(str1,temp1)&&0==strcmp(str2,temp2)){
printf("-1\n");
return;
}
strncpy(temp1,temp,len);
strncpy(temp2,temp+len,len);
temp1[len]=temp2[len]='\0';
for(int i=0,cnt=0;i<len;i++){
temp[cnt++]=temp2[i];
temp[cnt++]=temp1[i];
}
temp[len*2]='\0';
step++;
}
}
int main()
{
int N;
scanf("%d",&N);
for(int i=0;i<N;i++){
int len;
scanf("%d",&len);
cin>>str1>>str2>>str;
printf("%d ",i+1);
slove();
}
return 0;
}
H
这个题第一眼感觉就是分情况讨论。当时推了好久,但感觉很混乱而且太麻烦,没想到是BFS。
其实根据BFS的一些性质应该是能想到的:有限个确定的方式+树的宽度不大(数据不大)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
const int maxn=105;
int A,B,C;
struct Node{
int prea,preb,a,b,way;
}node[maxn][maxn];
//FILL(1) FILL(2) DROP(1) DROP(2)
//POUR(1,2) POUR(2,1)
void output(int x,int y,int n){
if(x==0&&y==0){
printf("%d\n",n);
return ;
}
output(node[x][y].prea,node[x][y].preb,n+1);
switch(node[x][y].way)
{
case 1: printf("FILL(1)\n"); break;
case 2: printf("FILL(2)\n"); break;
case 3: printf("DROP(1)\n"); break;
case 4: printf("DROP(2)\n"); break;
case 5: printf("POUR(1,2)\n"); break;
case 6: printf("POUR(2,1)\n"); break;
}
}
bool bfs()
{
queue<Node> q;
node[0][0]=(Node){-1,-1,0,0,-1};
q.push(node[0][0]);
while(!q.empty()){
Node now=q.front();
q.pop();
if(now.a==C||now.b==C){
output(now.a,now.b,0);
return true;
}
if(now.a!=A&&!node[A][now.b].way){
node[A][now.b]=(Node){now.a,now.b,A,now.b,1};
q.push(node[A][now.b]);
}
if(now.b!=B&&!node[now.a][B].way){
node[now.a][B]=(Node){now.a,now.b,now.a,B,2};
q.push(node[now.a][B]);
}
if(now.a!=0&&!node[0][now.b].way){
node[0][now.b]=(Node){now.a,now.b,0,now.b,3};
q.push(node[0][now.b]);
}
if(now.b!=0&&!node[now.a][0].way){
node[now.a][0]=(Node){now.a,now.b,now.a,0,4};
q.push(node[now.a][0]);
}
if(now.a!=0&&now.b!=B){
int a=now.a+now.b-B,b=now.a+now.b;
if(a<0) a=0;
if(b>B) b=B;
if(!node[a][b].way){
node[a][b]=(Node){now.a,now.b,a,b,5};
q.push(node[a][b]);
}
}
if(now.a!=A&&now.b!=0){
int a=now.a+now.b,b=now.a+now.b-A;
if(a>A) a=A;
if(b<0) b=0;
if(!node[a][b].way){
node[a][b]=(Node){now.a,now.b,a,b,6};
q.push(node[a][b]);
}
}
}
return false;
}
int main()
{
cin>>A>>B>>C;
if(!bfs()) printf("impossible");
return 0;
}
I:枚举两点bfs,只测了样例,福州大学的oj炸了
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
const int maxn=15;
char G[maxn][maxn],temp[maxn];
bool vis[maxn][maxn];
int N,M,to[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct Node{
int x,y,step;
};
bool isin(int x,int y){
if(x<1||y<1||x>N||y>M) return false;
if('.'==G[x][y]) return false;
if(vis[x][y]) return false;
return true;
}
int bfs(int x1,int y1,int x2,int y2,int cnt)
{
int ans=2;
queue<Node> q;
q.push((Node){x1,y1,0});
q.push((Node){x2,y2,0});
vis[x1][y1]=vis[x2][y2]=true;
int step=0;
while(!q.empty()){
Node now=q.front();
q.pop();
step=now.step;
for(int i=0;i<4;i++){
int nx=now.x+to[i][0],ny=now.y+to[i][1];
if(isin(nx,ny)){
vis[nx][ny]=true;
ans++;
q.push((Node){nx,ny,now.step+1});
}
}
}
if(ans==cnt) return step;
else return inf;
}
int main()
{
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++){
scanf("%d%d",&N,&M);
for(int j=1;j<=N;j++){
scanf("%s",temp);
sprintf(G[j],".%s",temp);
}
int cnt=0;
for(int j=1;j<=N;j++)
for(int k=1;k<=M;k++)
if('#'==G[j][k]) cnt++;
printf("Case %d: ",i);
if(cnt<=2) printf("0\n");
else{
int Min=inf;
for(int j=1;j<=N;j++){
for(int k=1;k<=M;k++){
if('#'==G[j][k]){
bool isfirst=true;
for(int t1=j;t1<=N;t1++){
for(int t2=isfirst?k+1:1;t2<=M;t2++){
if('#'==G[t1][t2]){
memset(vis,false,sizeof(vis));
Min=min(Min,bfs(j,k,t1,t2,cnt));
}
}
isfirst=false;
}
}
}
}
if(inf!=Min) printf("%d\n",Min);
else printf("-1\n");
}
}
return 0;
}
J:还做了一个多小时。。。
一:可能有多个火。。。
二:一次性将火放进队列进行,一次一次bfs会tle
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
const int maxn=1e3+5;
int t[maxn][maxn],N,M,to[4][2]={{1,0},{-1,0},{0,-1},{0,1}},Jx,Jy;
bool vis[maxn][maxn];
char G[maxn][maxn],temp[maxn];
struct Node{
int x,y,t;
};
bool isgo(int x,int y){
if('#'==G[x][y]) return false;
if(x<0||y<0||x>=N||y>=M) return false;
return true;
}
void bfs1()
{
queue<Node> q;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if('F'==G[i][j]){
q.push((Node){i,j,0});
t[i][j]=0;
}else{
t[i][j]=inf;
if('J'==G[i][j]) Jx=i,Jy=j;
}
vis[i][j]=false;
}
}
while(!q.empty()){
Node now=q.front();
q.pop();
for(int i=0;i<4;i++){
int nx=now.x+to[i][0],ny=now.y+to[i][1];
if(isgo(nx,ny)&&now.t+1<t[nx][ny]){
t[nx][ny]=now.t+1;
q.push((Node){nx,ny,now.t+1});
}
}
}
}
int bfs2(int x,int y)
{
queue<Node> q;
q.push((Node){x,y,0});
vis[x][y]=true;
while(!q.empty()){
Node now=q.front();
q.pop();
for(int i=0;i<4;i++){
int nx=now.x+to[i][0],ny=now.y+to[i][1];
if(nx<0||ny<0||nx>=N||ny>=M){
return now.t+1;
}else if('.'==G[nx][ny]&&now.t+1<t[nx][ny]&&!vis[nx][ny]){
q.push((Node){nx,ny,now.t+1});
vis[nx][ny]=true;
}
}
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
for(int i=0;i<N;i++)
scanf("%s",G[i]);
bfs1();
int step=bfs2(Jx,Jy);
if(-1==step) printf("IMPOSSIBLE\n");
else printf("%d\n",step);
}
return 0;
}
K:bfs
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int a[5][5];
bool visit[5][5];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
struct Node{
int x,y;
int s;
int l[30];
};
bool judge(int x,int y)
{
if(x<0||x>=5||y<0||y>=5)
return true;
if(visit[x][y])
return true;
if(a[x][y]==1)
return true;
return false;
}
Node& bfs()
{
queue<Node> que;
Node cur,next;
cur.x=0;cur.y=0;cur.s=0;
visit[0][0]=1;
que.push(cur);
while(que.size())
{
cur=que.front();
que.pop();
if(cur.x==4&&cur.y==4)
return cur;
int i,nx,ny;
for(i=0;i<4;i++)
{
nx=cur.x+dx[i];
ny=cur.y+dy[i];
if(judge(nx,ny))
continue;
next=cur;
next.x=nx;
next.y=ny;
next.s=cur.s+1;
next.l[cur.s]=i;
visit[nx][ny]=1;
que.push(next);
}
}
return cur;
}
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
scanf("%d",&a[i][j]);
}
}
Node ans=bfs();
printf("(0, 0)\n");
int x=0,y=0;
for(int i=0;i<ans.s;i++)
{
x+=dx[ans.l[i]];
y+=dy[ans.l[i]];
printf("(%d, %d)\n",x,y);
}
return 0;
}
L:DFS/BFS
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
using namespace std;
char dp[105][105];
int c; int k;
int bfs(int x,int y) {
int sum=0;
if (dp[x][y] == '@') {
sum++;
if (y < k) { dp[x][y] = '*'; sum = sum + bfs(x, y + 1); }
if (y > 0) { dp[x][y] = '*'; sum = sum + bfs(x, y - 1); }
if (x <c) { dp[x][y] = '*'; sum = sum + bfs(x + 1, y); }
if (x > 0) { dp[x][y] = '*'; sum = sum + bfs(x - 1, y); }
if (y < k&&x < c) { dp[x][y] = '*'; sum = sum + bfs(x+1, y + 1); }//右下
if (y < k&&x > 0) { dp[x][y] = '*'; sum = sum + bfs(x-1, y + 1); }//右上
if (y > 0 && x < c) { dp[x][y] = '*'; sum = sum + bfs(x+1, y - 1); }//左下
if (y > 0&&x > 0) { dp[x][y] = '*'; sum = sum + bfs(x-1, y - 1); }//左上
}
return sum;
}
int main() {
while(cin>>c>>k&&c&&k){
memset(dp,0,sizeof(dp));
int s1=0;
for(int i=0;i<c;i++){
for(int j=0;j<k;j++) {
cin>>dp[i][j];
}
}
for(int i=0;i<c;i++){
for(int j=0;j<k;j++){
if(bfs(i,j)>=1)s1++;
}
}
cout << s1<<endl;
}
return 0;
}
M 坑死人G++tle,C++AC,就是一个无脑的bfs
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define ll long long int
#define res register int
#define inf 0x3f3f3f3f
const int maxn=210;
int A,B,C;
bool vis[maxn][maxn][maxn];
struct Node{
int a,b,c;
int step;
};
int bfs()
{
queue<Node> q;
Node temp;
temp.a=A,temp.b=0,temp.c=0,temp.step=0;
q.push(temp);
vis[A][0][0]=true;
while(!q.empty()){
Node now=q.front();
int a=now.a,b=now.b,c=now.c,t=now.step;
q.pop();
if(a==A/2||b==A/2||c==A/2){
int cnt=0;
if(a==A/2) cnt++;
if(b==A/2) cnt++;
if(c==A/2) cnt++;
if(2==cnt) return t;
if(1==cnt) return t+1;
}
if(a!=0){//1
if(b!=B){
int temp=B-b;
if(a>temp){
a=a-temp;
b=B;
}else{
b=b+a;
a=0;
}
if(!vis[a][b][c]){
Node temp;
temp.a=a,temp.b=b,temp.c=c,temp.step=t+1;
q.push(temp);
vis[a][b][c]=true;
}
}
a=now.a,b=now.b,c=now.c,t=now.step;
if(c!=C){
int temp=C-c;
if(a>temp){
a=a-temp;
c=C;
}else{
c=c+a;
a=0;
}
if(!vis[a][b][c]){
Node temp;
temp.a=a,temp.b=b,temp.c=c,temp.step=t+1;
q.push(temp);
vis[a][b][c]=true;
}
}
}
a=now.a,b=now.b,c=now.c,t=now.step;
if(b!=0){//2
if(a!=A){
int temp=A-a;
if(b>temp){
b=b-temp;
a=A;
}else{
a=a+b;
b=0;
}
if(!vis[a][b][c]){
Node temp;
temp.a=a,temp.b=b,temp.c=c,temp.step=t+1;
q.push(temp);
vis[a][b][c]=true;
}
}
a=now.a,b=now.b,c=now.c,t=now.step;
if(c!=C){
int temp=C-c;
if(b>temp){
b=b-temp;
c=C;
}else{
c=c+b;
b=0;
}
if(!vis[a][b][c]){
Node temp;
temp.a=a,temp.b=b,temp.c=c,temp.step=t+1;
q.push(temp);
vis[a][b][c]=true;
}
}
}
a=now.a,b=now.b,c=now.c,t=now.step;
if(c!=0){//3
if(a!=A){
int temp=A-a;
if(c>temp){
c=c-temp;
a=A;
}else{
a=a+c;
c=0;
}
if(!vis[a][b][c]){
Node temp;
temp.a=a,temp.b=b,temp.c=c,temp.step=t+1;
q.push(temp);
vis[a][b][c]=true;
}
}
a=now.a,b=now.b,c=now.c,t=now.step;
if(b!=B){
int temp=B-b;
if(c>temp){
c=c-temp;
b=B;
}else{
b=c+b;
c=0;
}
if(!vis[a][b][c]){
Node temp;
temp.a=a,temp.b=b,temp.c=c,temp.step=t+1;
q.push(temp);
vis[a][b][c]=true;
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d%d",&A,&B,&C)&&A){
memset(vis,false,sizeof(vis));
if(A%2){
printf("NO\n");
continue;
}
int ans=bfs();
if(-1==ans) printf("NO\n");
else printf("%d\n",ans);
}
return 0;
}
N:之前做过这个题,肝不动了,贴个代码把。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxn=206;
const int INF=0x3f3f3f3f;
int dir[4][2]= {{0,1}, {0, -1}, {1, 0}, {-1, 0}};
int dist1[maxn][maxn], dist2[maxn][maxn];
int vis[maxn][maxn];
char maps[maxn][maxn];
int n, m;
struct node
{
int x, y, step;
};
void bfs(node start, int dist[][maxn])
{
memset(vis, 0, sizeof(vis));
queue<node>Q;
node p, q;
Q.push(start);
vis[start.x][start.y]=1;
dist[start.x][start.y]=0;
while(!Q.empty())
{
p=Q.front();
Q.pop();
for(int i=0; i<4; i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
if(!vis[q.x][q.y]&&q.x>=0&&q.x<n&&q.y>=0&&q.y<m&&maps[q.x][q.y]!='#')
{
vis[q.x][q.y]=1;
dist[q.x][q.y]=dist[p.x][p.y]+1;
Q.push(q);
}
}
}
}
int main()
{
while(~scanf("%d %d", &n, &m))
{
node s1, s2;
getchar();
for(int i=0; i<n; i++,getchar())
{
for(int j=0; j<m; j++)
{
scanf("%c", &maps[i][j]);
if(maps[i][j]=='Y')
{
s1.x=i;
s1.y=j;
s1.step=0;
}
if(maps[i][j]=='M')
{
s2.x=i;
s2.y=j;
s2.step=0;
}
}
}
memset(dist1, 0, sizeof(dist1));
memset(dist2, 0, sizeof(dist2));
bfs(s1, dist1);
bfs(s2, dist2);
int Min=INF;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(maps[i][j]=='@'&&dist1[i][j]!=0&&dist2[i][j]!=0)
Min=min(Min, dist1[i][j]+dist2[i][j]);
}
}
printf("%d\n", Min*11);
}
return 0;
}