ド素人のメモ帳

解いた問題のプログラムとか載せてくと思います。

AOJ0223

Stray Twins

■問題
もう一方と上下左右反対に移動する双子がいます。
二人の座標とデパートの情報が与えられた時二人が巡り合えるまでの最短距離を求めなさい。
最短距離が100以上になるときは"NA"と出力しなさい。


■解法
現在の距離、2人のx,y座標を持たせて幅優先探索するだけ。
二人の移動方向が逆なのと、やり方によってはMemory Limit Exceededになるのでフラグたてたり無駄な動きをなくしたり頑張る。
queueのpopの直後にフラグ立ててたのでMemory Limit Exceededだった。死にたい。


■ソース
デバック苦戦したため要らないとことかあって汚い。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef struct P{
  int tx,ty,kx,ky,c;
};
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
int d[55][55];
bool used[55][55][55][55];
int X,Y;
P fa;
int main(void){
  int ans;
  while(1){

    scanf("%d %d",&X,&Y);
    if(!X && !Y) break;
    scanf("%d %d %d %d",&fa.tx,&fa.ty,&fa.kx,&fa.ky);
    fa.c=0;
    for(int i=1;i<=Y;i++){
      for(int j=1;j<=X;j++){
	scanf("%d",&d[j][i]);
      }
    }


    ans=-1;
    memset(used,0,sizeof(used));
    queue<P> que;
    que.push(fa);

    while(!que.empty()){
      P p=que.front();
      que.pop();
      used[p.tx][p.ty][p.kx][p.ky]=true;
      if(p.c>100)break;
      if(p.tx==p.kx && p.ty==p.ky){
	ans=p.c;
	break;
      }
      if((abs(p.tx-p.kx)+abs(p.ty-p.ky)+1)/2>100-p.c)break;
      for(int i=0;i<4;i++){
	int f=0;
	int x[2]={p.tx,p.kx};
	int y[2]={p.ty,p.ky};
	for(int j=0;j<2;j++){
	  int t=1;
	  if(j)t=-1;
	  if(0<x[j]+dx[i]*t && x[j]+dx[i]*t<=X &&
              0<y[j]+dy[i]*t && y[j]+dy[i]*t<=Y){
	    if(!d[x[j]+dx[i]*t][y[j]+dy[i]*t]){
	      x[j]+=dx[i]*t;
	      y[j]+=dy[i]*t;
	      f++;
	    }
	  }
	}
	if(!f){
	  continue;
	}
	if(used[x[0]][y[0]][x[1]][y[1]]){
	  continue;
	}
	P pu={x[0],y[0],x[1],y[1],p.c+1};
	used[x[0]][y[0]][x[1]][y[1]]=true;
	que.push(pu);
      }
    }

    if(ans==-1)puts("NA");
    else printf("%d\n",ans);

  }
}