ac代码》》》》》》》》》》
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<queue>
#define inf 999999999
using namespace std;
int dp[110][110][33];
int map[110][110];
int n, m, k;
int nx[10],ny[10],sx,sy;
char str[110][110];
struct node
{
int x;
int y;
int s;
node( int xx=0,int yy=0,int ss=0 )
{
x=xx;
y=yy;
s=ss;
}
};
int mx[4]={ 1,-1,0,0};
int my[4]={ 0,0,-1,1};
int bfs()
{
int i,j;
queue<node>q;
int s=0;
for( i=0;i<k;i++ )
{
if( nx[i]==sx&&ny[i]==sy )
s=s|( 1<<i );
}
memset( dp,-1,sizeof( dp ) );
q.push( node( sx,sy,s ) );
dp[sx][sy][s]=0;
while( !q.empty() )
{
node now=q.front();
q.pop();
if( now.s==( ( 1<<k )-1 ) )
{
return dp[now.x][now.y][now.s];
}
for( i=0;i<4;i++ )
{
int xx=now.x+mx[i];
int yy=now.y+my[i];
int ss=now.s;
if( xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]==-2 )
continue;
for( j=0;j<k;j++ )
{
if( xx==nx[j]&&yy==ny[j] )
ss=ss|( 1<<j );
}
if( dp[xx][yy][ss]!=-1 )
continue;
dp[xx][yy][ss]=dp[now.x][now.y][now.s]+1;
q.push( node( xx,yy,ss ) );
}
}
return -1;
}
int main()
{
int i, j, t;
int x, y;
while( scanf( "%d%d",&n,&m ),n+m )
{
for( i=0;i<n;i++ )
scanf( "%s",str[i] );
memset( map,-1,sizeof( map ) );
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
if( str[i][j]=='@' )
{
sx=i;
sy=j;
}
if( str[i][j]=='#' )
map[i][j]=-2;
}
}
scanf( "%d",&k );
for( i=0;i<k;i++ )
{
scanf( "%d%d",&x,&y );
x--;
y--;
nx[i]=x;
ny[i]=y;
}
printf( "%d\n",bfs() );
}
return 0;
}
》》》》》》》》》》》》》》》》》》》》》ac代码》》》》》》》》》
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<queue>
#define inf 999999999
using namespace std;
struct node
{
int x;
int y;
};
char str[220][220];
int disss[220][220];
int dis[220][220];
int link[1220];
int used[122][120];
int visit[120];
int mx[4]={ 0,0,1,-1 };
int my[4]={ -1,1,0,0 };
int n, m, sum,countt,k,Mi;
node s[10],next,now;
int Min( int a , int b )
{
return a>b?b:a;
}
int ok( int x,int y )
{
if( x<0||x>=n||y<0||y>=m||str[x][y]=='#'||used[x][y] )
return 0;
return 1;
}
void dfs( int index,int sum,int layer )
{
int i;
if( layer==k )
{
Mi=Min( Mi,sum );
return ;
}
for( i=1;i<=k;i++ )
{
if( visit[i]==0&&i!=index )
{
visit[i]=1;
dfs( i,sum+disss[index][i],layer+1 );
visit[i]=0;
}
}
}
int bfs( node s,node t )
{
int i, j;
queue<node>q;
q.push( s );
while( !q.empty() )
{
now=q.front();
q.pop();
for( i=0;i<4;i++ )
{
next.x=now.x+mx[i];
next.y=now.y+my[i];
if( ok( next.x,next.y ) )
{
dis[next.x][next.y]=dis[now.x][now.y]+1;
if( next.x==t.x&&next.y==t.y )
return dis[next.x][next.y];
q.push( next );
}
used[next.x][next.y]=1;
}
}
return -1;
}
int main()
{
int i, j, t;
int x,y;
while( scanf( "%d%d",&n,&m ),n+m )
{
int flag=1;
sum=0;
for( i=0;i<n;i++ )
scanf( "%s",str[i] );
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
if( str[i][j]=='@' )
{
s[0].x=i;
s[0].y=j;
}
}
}
scanf( "%d",&k );
for( i=1;i<=k;i++ )
{
scanf( "%d%d",&x,&y );
s[i].x=x-1;
s[i].y=y-1;
}
for( i=0;i<=k;i++ )
{
for( j=i+1;j<=k;j++ )
{
memset( used,0,sizeof( used ) );
memset( dis,0,sizeof( dis ) );
disss[i][j]=disss[j][i]=bfs( s[i],s[j] );
if( dis[i][j]==-1 )
{
flag=0;
break;
}
}
if( flag==0 )
break;
}
if( flag )
{
Mi=inf;
sum=0;
memset( visit,0,sizeof( visit ) );
dfs( 0,0,0 );
printf ("%d\n",Mi );
}
else
printf( "-1\n" );
}
return 0;
}
《《《《《《《《《《《《《《《《《《《《《《《《《wa代码》》》》》》》》》》》》》
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<queue>
#define inf 999999999
using namespace std;
struct node
{
int x;
int y;
};
struct Node
{
int x;
int y;
int dis;
}disss[220];
int father[220];
char str[220][220];
int dis[220][220];
int link[1220];
int used[122][120];
int visit[120];
int mx[4]={ 0,0,1,-1 };
int my[4]={ -1,1,0,0 };
int n, m, sum,countt,k,Mi;
node s[10],next,now;
int cmp( const void * a ,const void * b )
{
return ( *( struct Node * )a).dis>( *( struct Node * )b ).dis?1:-1;
}
int Min( int a , int b )
{
return a>b?b:a;
}
int ok( int x,int y )
{
if( x<0||x>=n||y<0||y>=m||str[x][y]=='#'||used[x][y] )
return 0;
return 1;
}
void init( int n )
{
int i;
for( i=0;i<=n;i++ )
father[i]=i;
}
int find( int x )
{
return ( x==father[x] )?x:find( father[x] );
}
void Union( int x,int y )
{
int q,p;
q=find( x );
p=find( y );
if( q<p )
father[p]=q;
else
father[q]=p;
}
int bfs( node s,node t )
{
int i;
queue<node>q;
q.push( s );
while( !q.empty() )
{
now=q.front();
q.pop();
for( i=0;i<4;i++ )
{
next.x=now.x+mx[i];
next.y=now.y+my[i];
if( ok( next.x,next.y ) )
{
dis[next.x][next.y]=dis[now.x][now.y]+1;
if( next.x==t.x&&next.y==t.y )
return dis[next.x][next.y];
q.push( next );
}
used[next.x][next.y]=1;
}
}
return -1;
}
int main()
{
int i, j, t, q;
int x,y;
while( scanf( "%d%d",&n,&m ),n+m )
{
int flag=1;
sum=0;
for( i=0;i<n;i++ )
scanf( "%s",str[i] );
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
if( str[i][j]=='@' )
{
s[0].x=i;
s[0].y=j;
}
}
}
scanf( "%d",&k );
for( i=1;i<=k;i++ )
{
scanf( "%d%d",&x,&y );
s[i].x=x-1;
s[i].y=y-1;
}
q=0;
for( i=0;i<=k;i++ )
{
for( j=i+1;j<=k;j++ )
{
memset( used,0,sizeof( used ) );
memset( dis,0,sizeof( dis ) );
disss[q].dis=bfs( s[i],s[j] );
disss[q].x=i;
disss[q].y=j;
if( disss[q].dis==-1 )
{
flag=0;
break;
}
q++;
}
if( flag==0 )
break;
}
if( flag )
{
countt=0;
sum=0;
init( k );
qsort( disss,q,sizeof( disss[0] ),cmp );
for( i=0;i<q;i++ )
{
if( find( disss[i].x )!=find( disss[i].y ) )( 没考虑重复路径 !!!!!!!!!!!!!!!!!!!!!!》
{
Union( disss[i].x,disss[i].y );
countt++;
sum+=disss[i].dis;
}
if( countt==k )
break;
}
printf ("%d\n",sum );
}
else
printf( "-1\n" );
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<queue>
#define inf 999999999
using namespace std;
int dp[110][110][33];
int map[110][110];
int n, m, k;
int nx[10],ny[10],sx,sy;
char str[110][110];
struct node
{
int x;
int y;
int s;
node( int xx=0,int yy=0,int ss=0 )
{
x=xx;
y=yy;
s=ss;
}
};
int mx[4]={ 1,-1,0,0};
int my[4]={ 0,0,-1,1};
int bfs()
{
int i,j;
queue<node>q;
int s=0;
for( i=0;i<k;i++ )
{
if( nx[i]==sx&&ny[i]==sy )
s=s|( 1<<i );
}
memset( dp,-1,sizeof( dp ) );
q.push( node( sx,sy,s ) );
dp[sx][sy][s]=0;
while( !q.empty() )
{
node now=q.front();
q.pop();
if( now.s==( ( 1<<k )-1 ) )
{
return dp[now.x][now.y][now.s];
}
for( i=0;i<4;i++ )
{
int xx=now.x+mx[i];
int yy=now.y+my[i];
int ss=now.s;
if( xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]==-2 )
continue;
for( j=0;j<k;j++ )
{
if( xx==nx[j]&&yy==ny[j] )
ss=ss|( 1<<j );
}
if( dp[xx][yy][ss]!=-1 )
continue;
dp[xx][yy][ss]=dp[now.x][now.y][now.s]+1;
q.push( node( xx,yy,ss ) );
}
}
return -1;
}
int main()
{
int i, j, t;
int x, y;
while( scanf( "%d%d",&n,&m ),n+m )
{
for( i=0;i<n;i++ )
scanf( "%s",str[i] );
memset( map,-1,sizeof( map ) );
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
if( str[i][j]=='@' )
{
sx=i;
sy=j;
}
if( str[i][j]=='#' )
map[i][j]=-2;
}
}
scanf( "%d",&k );
for( i=0;i<k;i++ )
{
scanf( "%d%d",&x,&y );
x--;
y--;
nx[i]=x;
ny[i]=y;
}
printf( "%d\n",bfs() );
}
return 0;
}
》》》》》》》》》》》》》》》》》》》》》ac代码》》》》》》》》》
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<queue>
#define inf 999999999
using namespace std;
struct node
{
int x;
int y;
};
char str[220][220];
int disss[220][220];
int dis[220][220];
int link[1220];
int used[122][120];
int visit[120];
int mx[4]={ 0,0,1,-1 };
int my[4]={ -1,1,0,0 };
int n, m, sum,countt,k,Mi;
node s[10],next,now;
int Min( int a , int b )
{
return a>b?b:a;
}
int ok( int x,int y )
{
if( x<0||x>=n||y<0||y>=m||str[x][y]=='#'||used[x][y] )
return 0;
return 1;
}
void dfs( int index,int sum,int layer )
{
int i;
if( layer==k )
{
Mi=Min( Mi,sum );
return ;
}
for( i=1;i<=k;i++ )
{
if( visit[i]==0&&i!=index )
{
visit[i]=1;
dfs( i,sum+disss[index][i],layer+1 );
visit[i]=0;
}
}
}
int bfs( node s,node t )
{
int i, j;
queue<node>q;
q.push( s );
while( !q.empty() )
{
now=q.front();
q.pop();
for( i=0;i<4;i++ )
{
next.x=now.x+mx[i];
next.y=now.y+my[i];
if( ok( next.x,next.y ) )
{
dis[next.x][next.y]=dis[now.x][now.y]+1;
if( next.x==t.x&&next.y==t.y )
return dis[next.x][next.y];
q.push( next );
}
used[next.x][next.y]=1;
}
}
return -1;
}
int main()
{
int i, j, t;
int x,y;
while( scanf( "%d%d",&n,&m ),n+m )
{
int flag=1;
sum=0;
for( i=0;i<n;i++ )
scanf( "%s",str[i] );
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
if( str[i][j]=='@' )
{
s[0].x=i;
s[0].y=j;
}
}
}
scanf( "%d",&k );
for( i=1;i<=k;i++ )
{
scanf( "%d%d",&x,&y );
s[i].x=x-1;
s[i].y=y-1;
}
for( i=0;i<=k;i++ )
{
for( j=i+1;j<=k;j++ )
{
memset( used,0,sizeof( used ) );
memset( dis,0,sizeof( dis ) );
disss[i][j]=disss[j][i]=bfs( s[i],s[j] );
if( dis[i][j]==-1 )
{
flag=0;
break;
}
}
if( flag==0 )
break;
}
if( flag )
{
Mi=inf;
sum=0;
memset( visit,0,sizeof( visit ) );
dfs( 0,0,0 );
printf ("%d\n",Mi );
}
else
printf( "-1\n" );
}
return 0;
}
《《《《《《《《《《《《《《《《《《《《《《《《《wa代码》》》》》》》》》》》》》
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<queue>
#define inf 999999999
using namespace std;
struct node
{
int x;
int y;
};
struct Node
{
int x;
int y;
int dis;
}disss[220];
int father[220];
char str[220][220];
int dis[220][220];
int link[1220];
int used[122][120];
int visit[120];
int mx[4]={ 0,0,1,-1 };
int my[4]={ -1,1,0,0 };
int n, m, sum,countt,k,Mi;
node s[10],next,now;
int cmp( const void * a ,const void * b )
{
return ( *( struct Node * )a).dis>( *( struct Node * )b ).dis?1:-1;
}
int Min( int a , int b )
{
return a>b?b:a;
}
int ok( int x,int y )
{
if( x<0||x>=n||y<0||y>=m||str[x][y]=='#'||used[x][y] )
return 0;
return 1;
}
void init( int n )
{
int i;
for( i=0;i<=n;i++ )
father[i]=i;
}
int find( int x )
{
return ( x==father[x] )?x:find( father[x] );
}
void Union( int x,int y )
{
int q,p;
q=find( x );
p=find( y );
if( q<p )
father[p]=q;
else
father[q]=p;
}
int bfs( node s,node t )
{
int i;
queue<node>q;
q.push( s );
while( !q.empty() )
{
now=q.front();
q.pop();
for( i=0;i<4;i++ )
{
next.x=now.x+mx[i];
next.y=now.y+my[i];
if( ok( next.x,next.y ) )
{
dis[next.x][next.y]=dis[now.x][now.y]+1;
if( next.x==t.x&&next.y==t.y )
return dis[next.x][next.y];
q.push( next );
}
used[next.x][next.y]=1;
}
}
return -1;
}
int main()
{
int i, j, t, q;
int x,y;
while( scanf( "%d%d",&n,&m ),n+m )
{
int flag=1;
sum=0;
for( i=0;i<n;i++ )
scanf( "%s",str[i] );
for( i=0;i<n;i++ )
{
for( j=0;j<m;j++ )
{
if( str[i][j]=='@' )
{
s[0].x=i;
s[0].y=j;
}
}
}
scanf( "%d",&k );
for( i=1;i<=k;i++ )
{
scanf( "%d%d",&x,&y );
s[i].x=x-1;
s[i].y=y-1;
}
q=0;
for( i=0;i<=k;i++ )
{
for( j=i+1;j<=k;j++ )
{
memset( used,0,sizeof( used ) );
memset( dis,0,sizeof( dis ) );
disss[q].dis=bfs( s[i],s[j] );
disss[q].x=i;
disss[q].y=j;
if( disss[q].dis==-1 )
{
flag=0;
break;
}
q++;
}
if( flag==0 )
break;
}
if( flag )
{
countt=0;
sum=0;
init( k );
qsort( disss,q,sizeof( disss[0] ),cmp );
for( i=0;i<q;i++ )
{
if( find( disss[i].x )!=find( disss[i].y ) )( 没考虑重复路径 !!!!!!!!!!!!!!!!!!!!!!》
{
Union( disss[i].x,disss[i].y );
countt++;
sum+=disss[i].dis;
}
if( countt==k )
break;
}
printf ("%d\n",sum );
}
else
printf( "-1\n" );
}
return 0;
}