Re: 929 - Number Maze
Posted: Tue Feb 03, 2009 9:35 pm
He is correct, I am not sure why you don't believe him. What does flag[j-1]==0 evaluate to if j==0?
Code: Select all
if(j>0 && flag[i][j-1]==0)
and also,
if(j>0)
if(flag[i][j-1]==0)
Code: Select all
while(i!=n && j!=m)(
Code: Select all
while (true) {
temp = a[1].data;
i = a[1].x;
j = a[1].y;
if (i == n - 1 && j == m - 1)
break;
......
Code: Select all
#include <stdio.h>
#include <memory.h>
#include <limits.h>
struct CNum
{
int number;
int x, y;
};
struct move
{
int x, y;
};
class CQueue
{
public :
CNum data[1000000];
int front, rear;
inline void push(int dat, int x, int y)
{
data[rear].number = dat;
data[rear].x = x;
data[rear].y = y;
rear++;
}
inline CNum pop()
{
if(front <= rear)
return data[front++];
}
CQueue()
{
clear();
}
inline void clear()
{
front = rear = 0;
}
};
int maze[1000][1000], distance[1000][1000];
CQueue Q[10];
char buffer [50000];
int main()
{
move moves[4], t;
int cases, row, col, i, j, idxmin, row1, col1, metu;
char *ctr;
CNum MNow;
freopen("f.in", "r", stdin);
freopen("f.out", "w", stdout);
moves[0].x = -1;
moves[0].y = 0;
moves[1].x = 1;
moves[1].y = 0;
moves[2].x = 0;
moves[2].y = -1;
moves[3].x = 0;
moves[3].y = 1;
scanf("%i\n", &cases);
while(cases--)
{
for(i = 0 ; i < 10; i++)
{
Q[i].clear();
}
scanf("%i\n%i\n", &row, &col);
for(i = 0; i < row; i++)
{
gets(buffer);
ctr = buffer;
for(j = 0; j < col; j++)
{
while(*ctr == ' ') ctr++;
maze[i][j] = *ctr - '0';
distance[i][j] = INT_MAX;
ctr++;
}
}
row1 = row - 1;
col1 = col - 1;
// BFS
distance[0][0] = maze[0][0];
Q[0].push(maze[0][0],0,0);
idxmin = 0;
metu = 0;
while(!metu)
{
MNow = Q[idxmin].pop();
//printf("%i %i %i\n", MNow.x, MNow.y, MNow.number);
for(i = 0; i < 4; i++)
{
t.x = MNow.x + moves[i].x;
t.y = MNow.y + moves[i].y;
if(t.x >= 0 && t.x < col &&
t.y >= 0 && t.y < row)
{
if(distance[t.y][t.x] > MNow.number + maze[t.y][t.x])
{
if(t.y == row1 && t.x == col1)
{
printf("%i\n", MNow.number + maze[t.y][t.x]);
metu = 1;
break;
}
else
{
distance[t.y][t.x] = MNow.number + maze[t.y][t.x];
Q[distance[t.y][t.x] % 10].push(distance[t.y][t.x], t.x, t.y);
}
}
}
}
if(!metu)
{
// Get Highest Priority
for(i = 0; i < 10; i++)
{
if(Q[i].front != Q[i].rear)
{
idxmin = i;
break;
}
}
for(i = i + 1; i < 10; i++)
{
if( (Q[i].front != Q[i].rear) &&
Q[i].data[Q[i].front].number < Q[idxmin].data[Q[idxmin].front].number)
{
idxmin = i;
}
}
}
}
}
return 0;
}
Code: Select all
// find bugs,code remove
Code: Select all
4
1 1
9
2 2
0 0
0 0
20 20
1 4 5 7 8 9 7 5 3 4 6 7 4 3 6 8 4 3 5 9
2 4 0 9 7 5 4 6 7 8 4 3 1 4 6 7 8 9 6 5
1 2 3 4 6 6 8 9 9 0 0 0 0 0 3 4 5 4 3 1
1 4 5 7 8 9 7 5 3 4 6 7 4 3 6 8 4 3 5 9
2 4 0 9 7 5 4 6 7 8 4 3 1 4 6 7 8 9 6 5
1 2 3 4 5 6 7 8 9 9 0 6 4 3 2 4 5 6 7 8
8 5 9 2 4 5 6 7 8 9 2 1 2 3 4 5 6 8 9 0
9 9 9 0 0 0 0 0 1 2 3 4 6 7 8 9 0 5 4 3
1 2 3 4 5 6 7 8 9 9 0 6 4 3 2 4 5 6 7 8
1 4 5 7 8 9 7 5 3 4 6 7 4 3 6 8 4 3 5 9
1 3 5 7 8 9 2 2 2 2 3 4 4 4 4 5 6 7 8 9
0 9 9 8 7 6 9 9 9 9 9 9 9 0 0 0 0 0 9 9
1 2 3 4 5 6 7 8 9 9 0 6 4 3 2 4 5 6 7 8
8 5 9 2 4 5 6 7 8 9 2 1 2 3 4 5 6 8 9 0
1 2 3 4 6 6 8 9 9 0 0 0 0 0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 4 5 6 7 8 9 2 1 2 3 4 5 6 8 9 0
1 2 3 4 6 6 8 9 9 0 0 0 0 0 3 4 5 4 3 1
Code: Select all
9
0
75
67
Code: Select all
//cut after getting accepted
Code: Select all
#include<cstdio>
#include<cstring>
using namespace std;
int check[1005][1005];
int inp[1005][1005];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
int dis[1005][1005];
typedef struct{
int x,y,w;
}N;
N queue[1000000];
int main()
{
int r,c,i,j,k,t,xx,yy,start,end,min,pos;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&r,&c);
memset(check,false,sizeof(check));
for(i=0; i<r; i++)
{
for(j=0; j<c; j++) scanf("%d",&inp[i][j]);
}
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
dis[i][j]=2474836;
}
}
N s,p,s1,temp;
queue[0].x=0;queue[0].y=0;queue[0].w=inp[0][0];
dis[0][0]=inp[0][0];
start=end=0;
end++;
while(start<end)
{
min=queue[start].w;
pos=start;
for(j=start+1; j<end; j++)/*find the smallest element,and swap it with start pos element*/
{
if(min>queue[j].w)
{
min=queue[j].w;
pos=j;
}
}
temp=queue[pos];
queue[pos]=queue[start];
queue[start]=temp;
s=queue[start];
check[s.y][s.x]=true;
if(s.y==r-1 && s.x==c-1) break;
for(i=0; i<4; i++)
{
xx=s.x+dx[i];
yy=s.y+dy[i];
if(xx<0||yy<0||xx>c-1||yy>r-1) continue;
if(check[yy][xx]==true) continue;
if(dis[yy][xx]>dis[s.y][s.x]+inp[yy][xx])
{
dis[yy][xx]=dis[s.y][s.x]+inp[yy][xx];
}
queue[end].x=xx;queue[end].y=yy;queue[end].w=dis[yy][xx];
end++;
}
start++;
}
printf("%d\n",dis[r-1][c-1]);
}
return 0;
}