Jizhong training 2020.01.14 [group] NOIP popular simulation game in group C ------ Xiaoming game summary
3. Xiaoming game
I hold the mentality to try to open this topic point, looked alarmed topic, actually, and group B hit title, panic, and not much time, make it violent, take 40 minutes enough. Do not tell me violence 0!
Then avoid being the perfect solution, the perfect TLE a perfect write recursive!
------- -------- gorgeous dividing line
Closer to home
Subject to the effect: Xiao Ming recently like to play a game. Given an n * m board, and the above two grid # @. Rules of the game is simple: Given a starting location and a destination location, Xiaoming each step can be up, down, left, and right direction one space. If you move to the same type of lattice, the fee is zero, otherwise the cost is 1. Calculates the movement program a minimum cost from the start position to the target position.
Input
Input file multiple sets of data.
Input of the first line contains two integers n, m, respectively, number of rows and columns of the board.
Enter the next n rows, each row has m lattice (or using # @ represented).
The next input row has four integers x1, y1, x2, y2, respectively, a starting position and the target position.
When the input n, m are both 0, indicating the end of input.
Output
For each test, the output takes the minimum from a start position to a target position. Each set of data on a separate line.
The Input the Sample
2 2
@ #
# @
0 0 1 1
2 2
@@
@ #
0 1 1 0
0 0
the Sample the Output
2
0
This question is not a far-fetched advanced algorithms such as shortest path, or add any advanced optimization. But in fact is a memory search of water problem ah! God (heart) clear (state) gas (collapse) cool (collapse)! ! ! ! ! 100 ah! We gave 60 points!
Analysis: In fact, this question is to look at the border, can not change the letters to try not to change the letter, is seeking to spend, rather than shortest distance. Make a wide search of memory, and then open a large array, open to both AC and not empty over the point where you can! The core test sites actually ------------- pressure space! The WHAT!
Attach Pascal AC Code:
uses math;
var
n,m,i,j,x1,x2,y1,y2,minn:longint;
map:array[0..1005,0..1005] of char;
fx:array[1..4] of longint=(-1,0,1,0);
fy:array[1..4] of longint=(0,-1,0,1);
f:array[0..1005,0..1005] of longint;
a:array[0..5000005,1..2] of longint;
procedure bfs(x,y:longint);
var
head,tail,xx,yy:longint;
begin
head:=0;
tail:=1;
a[tail,1]:=x;
a[tail,2]:=y;
while head<tail do
begin
inc(head);
for i:=1 to 4 do
begin
xx:=a[head,1]+fx[i];
yy:=a[head,2]+fy[i];
if (xx>=0) and (yy>=0) and (xx<n) and (yy<m) then
begin
if map[xx,yy]=map[a[head,1],a[head,2]] then
begin
if f[a[head,1],a[head,2]]<f[xx,yy] then
begin
inc(tail);
a[tail,1]:=xx;
a[tail,2]:=yy;
f[xx,yy]:=f[a[head,1],a[head,2]];
end;
end
else
begin
if f[a[head,1],a[head,2]]+1<f[xx,yy] then
begin
inc(tail);
a[tail,1]:=xx;
a[tail,2]:=yy;
f[xx,yy]:=f[a[head,1],a[head,2]]+1;
end;
end;
end;
end;
end;
writeln(f[x2,y2]);
end;
begin
readln(n,m);
while (n<>0) or (m<>0) do
begin
for i:=0 to n-1 do
begin
for j:=0 to m-1 do
begin
read(map[i,j]);
f[i,j]:=maxlongint div 2;
end;
readln;
end;
readln(x1,y1,x2,y2);
f[x1,y1]:=0;
bfs(x1,y1);
readln(n,m);
end;
end.
Attach C ++ AC Code:
#include<cstdio>
#define inf 2100000000
using namespace std;
char map[505][505];
int f[505][505],a[5000005][3];
int ans,n,m,sx,sy,ex,ey,head,tail,x,y,xx,yy;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main()
{
scanf("%d%d",&n,&m);
while (n!=0||m!=0)
{
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
map[i][j]=getchar();
while (map[i][j]!='#'&&map[i][j]!='@') map[i][j]=getchar();
f[i][j]=inf;
}
}
scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
head=0;
tail=1;
a[1][1]=sx;
a[1][2]=sy;
a[1][0]=0;
ans=inf;
while (head<tail)
{
head++;
xx=a[head][1];
yy=a[head][2];
if (a[head][0]>ans)
continue;
for (int i=0;i<4;i++)
{
x=xx+dx[i];
y=yy+dy[i];
if (x>=0&&x<n&&y>=0&&y<m)
{
if (map[xx][yy]!=map[x][y]&&a[head][0]+1>=f[x][y]||map[xx][yy]==map[x][y]&&a[head][0]>=f[x][y]) continue;
if (map[xx][yy]==map[x][y])
a[++tail][0]=a[head][0];
else
a[++tail][0]=a[head][0]+1;
if (a[tail][0]<f[x][y])
f[x][y]=a[tail][0];
a[tail][1]=x;
a[tail][2]=y;
if(x==ex&&y==ey)
if (ans>a[tail][0])
ans=a[tail][0];
}
}
}
printf("%d\n",ans);
scanf("%d%d",&n,&m);
}
return 0;
}