Hangzhou Electric 1026

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1026

Submitted several times, with Dfs timeout, but also with BFS, but the memory is super.

Handed several times or do not know how to solve, later only to know, need a vis array to represent the points have been queued, to solve the problem of the queue.

The code is as follows:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define MAX 0XFFFFFFF
#include <queue>

struct node{
int x, y;
}o;
using namespace Std;

int map[110][110],dis[110][110],vis[110][110];
int n,m;
Node front[110][110];
int m_x[]={-1,0,1,0},m_y[]={0,1,0,-1};

void BFs () {
for (int i=0;i<110;i++)
for (int j=0;j<110;j++)
Dis[i][j]=max;
dis[0][0]=0;
Queue<node> Q;
o.x=0;o.y=0;
Q.push (o);
Vis[0][0]=1;
while (!q.empty ())
{
node tmp;
Tmp=q.front ();
Q.pop ();
vis[tmp.x][tmp.y]=0;
for (int i=0;i<4;i++)
{
int tx=tmp.x+m_x[i],ty=tmp.y+m_y[i];
if (tx>=0 && tx<n && ty>=0 && ty<m)
if (Map[tx][ty]!=-1 && (Dis[tmp.x][tmp.y]+map[tx][ty]) <dis[tx][ty])
{
dis[tx][ty]=dis[tmp.x][tmp.y]+map[tx][ty]+1;//cout<<tx<< "" <<ty<< "<<dis[tx][ty" <<endl;
front[tx][ty].x=tmp.x;
FRONT[TX][TY].Y=TMP.Y;
O.x=tx;o.y=ty;
if (!vis[tx][ty]) {
Q.push (o);
Vis[tx][ty]=1;
}
}
}
}
}


void disp (int n,int m,int t) {
if (front[n][m].x+front[n][m].y==0)
{
printf ("%ds: (%d,%d) (%d,%d) \ n", 1,front[n][m].x,front[n][m].y,n,m);
for (int i=0;i<map[n][m];i++)
{
printf ("%ds:fight at (%d,%d) \ n", i+2,n,m);
}
Return
}
Else
{
Disp (front[n][m].x,front[n][m].y,t-map[n][m]-1);
printf ("%ds: (%d,%d) (%d,%d) \ n", t-map[n][m],front[n][m].x,front[n][m].y,n,m);
for (int i=0;i<map[n][m];i++)
printf ("%ds:fight at (%d,%d) \ n", t+i+1-map[n][m],n,m);
}
}
int main () {
while (Cin>>n)
{
cin>>m;
Char Ch;getchar ();
memset (vis,0,sizeof (VIS));
for (int i=0;i<n;i++) {
for (int j=0;j<m;j++)
{
cin>>ch;
if (ch== ' X ') map[i][j]=-1;
else if (ch== '. ') map[i][j]=0;
else map[i][j]=ch-' 0 ';
}
GetChar ();
}
BFS ();
if (Dis[n-1][m-1]==max) printf ("God's help to our poor hero.\n");
else{
printf ("It takes%d seconds to reach the target position, let me show you the way.\n", Dis[n-1][m-1]);
Disp (n-1,m-1,dis[n-1][m-1]);
}
cout<< "finish\n";
}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.