古老的显示屏
首先是简单的宽搜
每次找出最优的距离顶点(距离指该定点到亮点的最短距离)去松弛四周的顶点。
如果这个顶点自己是亮的,那么他的最短距离是0
不详细说,只是个大概的思路
#include<stdio.h>
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
const int MAX_N = 201;
const int MAX_M = 4;
const int INF = 1000000;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int G[MAX_N][MAX_N];
int f[MAX_N][MAX_N];
int N,M;
struct node
{
int x,y,c;
}head,next;
queue<struct node> Q;
bool in_G(int x,int y)
{
if (x<1||x>N) return false;
if (y<1||y>M) return false;
return true;
}
int init()
{
int i,j;
char c;
scanf("%d %d",&N,&M);
for(i=1;i<=N;i++)
{
scanf("\n");
for (j=1;j<=M;j++)
{
scanf("%c",&c);
G[i][j]=c-'0';
f[i][j]=INF;
if (G[i][j])
{
head.x=i;
head.y=j;
f[i][j]=head.c=0;
Q.push(head);
}
}
}
}
int work()
{
while (!Q.empty())
{
head=Q.front();
Q.pop();
int i;
for(i=0;i<MAX_M;i++)
{//(1,1)->,(1,2),(2,1)
// c c+1 c+1 c+1 c+1
///\ k
// <=c+1
int newx=head.x+dx[i];
int newy=head.y+dy[i];
int newc=head.c+1;
if (in_G(newx,newy)&&newc<f[newx][newy])
{
f[newx][newy]=newc;
next.x=newx;
next.y=newy;
next.c=newc;
Q.push(next);
}
}
}
}
int put()
{
int i,j;
for (i=1;i<=N;i++)
{
for (j=1;j<=M;j++)
printf("%d ",f[i][j]);
printf("\n");
}
}
int main()
{
freopen("bit.in","r",stdin);
freopen("bit.out","w",stdout);
init();
work();
put();
return 0;
}
迷宫是dfs,无难度,大概是普及组第二题-的样子。
但是普及组不会考这么简单的dfs...
不用任何优化,就是个暴力。
#include<stdio.h>
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
const int MAX_N = 6;
const int MAX_M = 4;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
bool G[MAX_N][MAX_N];
bool used[MAX_N][MAX_N];
int N,M,T;
int Sx,Sy,Tx,Ty;
int ans;
bool in_G(int x,int y)
{
if (x<1||x>N) return false;
if (y<1||y>M) return false;
return true;
}
int init()
{
int i,j;
int x,y;
scanf("%d %d %d",&N,&M,&T);
scanf("%d %d %d %d",&Sx,&Sy,&Tx,&Ty);
memset(G,true,sizeof(G));
memset(used,true,sizeof(used));
for (i=1;i<=T;i++)
scanf("%d %d",&x,&y),G[x][y]=false;
}
int dfs(int x,int y)
{
if (!in_G(x,y)) return 0;
if (!used[x][y]) return 0;
if (!G[x][y]) return 0;
if (x==Tx&&y==Ty) {ans++;return 0;}
int i;
used[x][y]=false;
for (i=0;i<MAX_M;i++)
if (used[x+dx[i]][y+dy[i]]&&G[x+dx[i]][y+dy[i]])
dfs(x+dx[i],y+dy[i]);
used[x][y]=true;
}
int put()
{
printf("%d",ans);
}
int main()
{
freopen("maze.in","r",stdin);
freopen("maze.out","w",stdout);
init();
dfs(Sx,Sy);
put();
return 0;
}
玉米迷宫有点难,暂时不解释。宋老师估计N年以后会说的。
#include<stdio.h>
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
const int MAX_N = 1001;
const int MAX_M = 4;
const int INF = 10000000;
int dx[]={0,0,-1,1};
int dy[]={-1,1,0,0};
int f[MAX_N][MAX_N];
char G[MAX_N][MAX_N];
struct node
{
int x,y,c;
}head,next,cs[MAX_N];
int N,M;
int Sx,Sy;
int ans;
queue <struct node> Q;
bool in_G(int x,int y)
{
if (x<1||x>N) return false;
if (y<1||y>M) return false;
return true;
}
int init()
{
int i,j;
char c;
scanf("%d %d",&N,&M);
for(i=1;i<=N;i++)
{
scanf("\n");
for (j=1;j<=M;j++)
{
scanf("%c",&G[i][j]);
if (G[i][j]=='@') Sx=i,Sy=j;
if (G[i][j]>='A'&&G[i][j]<='Z')
{
int c=G[i][j];
if (cs[c].x!=0)
c=c-'A'+'a';
cs[c].x=i;
cs[c].y=j;
}
f[i][j]=INF;
}
}
f[Sx][Sy]=0;
head.x=Sx;
head.y=Sy;
head.c=0;
Q.push(head);
}
int bfs()
{
while(!Q.empty())
{
head=Q.front();
Q.pop();
if (G[head.x][head.y]=='=')
{
ans=f[head.x][head.y]=min(f[head.x][head.y],head.c);
continue;
}
if (G[head.x][head.y]>='A'&&G[head.x][head.y]<='Z')
{
int c=G[head.x][head.y];
if (cs[c].x==head.x&&cs[c].y==head.y) c=c-'A'+'a';
head.x=cs[c].x;
head.y=cs[c].y;
}
int i;
for (i=0;i<MAX_M;i++)
{
int newx=head.x+dx[i];
int newy=head.y+dy[i];
int newc=head.c+1;
if (newc<f[newx][newy]&&G[newx][newy]!='#')
{
f[newx][newy]=newc;
next.x=newx;
next.y=newy;
next.c=newc;
Q.push(next);
}
}
}
}
int put()
{
printf("%d",ans);
}
int main()
{
freopen("cornmaze.in","r",stdin);
freopen("cornmaze.out","w",stdout);
init();
bfs();
put();
return 0;
}