Sicily 1215 out of the dungeon

Source: Internet
Author: User
Tags creative commons attribution

do Sicily 1215 out of the dungeon this problem, has been restrict function, are almost collapsed.
The last discovery is a memory leak, that is, something new comes out without a timely delete result.
in the breadth of the first search, each neighbor to new out some nodes, some nodes have been visited and then I directly ignore them, in fact, you have to delete them, and the pop out of a node, after processing the neighbor to delete the node
Put a code snippet: 32nd, line 35 is the place to pay attention to. Operating efficiency: 0 seconds, 312KB the complete code is as follows:
problem#: 1215//submission#: 2642938//The source code is licensed under Creative Commons Attribution-noncommercial-sh Arealike 3.0 Unported license//uri:http://creativecommons.org/licenses/by-nc-sa/3.0///all Copyright reserved by Informatic Lab of Sun Yat-sen university#include<iostream> #include <stdio.h> #include <queue> # include<cstring> #include <vector> #include <algorithm> #include <cmath>using namespace std; Char **prison;bool *visited;unsigned char *dotperline = null;unsigned char **onelinedot = null;int N, M;char dirParis[4][2    ] = {{ -1,0},{1,0},{0,-1},{0,1}};char dirhelen[4][2];int dotcount;struct node{int px;    int py;    int HX;    int hy;    int step;        Node () {step = 0;    parent = NULL; } node* parent;};            int hashpoint (int x, int y) {int p=0; p+=dotperline[x-1];//for (int j = 0; J < y; j + +)//{//if (prison[x][j] = = '. ' | | prison[x][j] = = ' H ' | | | prison[x ][J] = = ' P ')//p++;//}//      P+=ONELINEDOT[X][Y-1]; return p;} int Hashnode (node* Node) {return hashpoint (node->hx-1,node->hy-1) *dotcount+hashpoint (node->px-1,node-> PY-1); }//bool CMP (const node* A, const node* b)//{//int disA = ABS (A-&GT;PX-A-&GT;HX) +abs (a->py-a->hy);//int DISB = a BS (B-&GT;PX-B-&GT;HX) +abs (b->py-b->hy);//Return DisA < disb;//}vector<node*> Getadj (node* Node) {Vect      or<node*> ret;         for (int i = 0; i < 4; i++) {Char pnext = prison[node->px-1+dirparis[i][0]][node->py-1+dirparis[i][1]];        Char Hnext = prison[node->hx-1+dirhelen[i][0]][node->hy-1+dirhelen[i][1]];        if (pnext! = ' # ' && pnext! = '! ')            {if (Hnext! = '! ')                               {node* NewNode = new Node;                NEWNODE-&GT;PX = node->px+dirparis[i][0];                              Newnode->py = node->py+dirparis[i][1];              if (Hnext = = ' # ') {      NEWNODE-&GT;HX = node->hx;                Newnode->hy = node->hy;                    } else {newnode->hx = node->hx+dirhelen[i][0];                Newnode->hy = node->hy+dirhelen[i][1];                } Newnode->step = node->step+1;                Ret.push_back (NewNode);            newnode->parent = node; }}} return ret;} BOOL Ispass (node* Node) {return node->px = = Node->hx && node->py = Node->hy | | node->px = = Node->parent->hx && node->py = = Node->parent->hy && Node->hx = node->parent-> PX && Node->hy = = node->parent->py;}    int BFS (node* Node) {queue<node*> Q;    Q.push (node); while (!        Q.empty ()) {node* Node = Q.front ();        Q.pop ();           vector<node*> adj = Getadj (Node);        Sort (Adj.begin (), Adj.end (), CMP); for (inti = 0; I < adj.size ();            i++) {node* child = adj.at (i);            if (Child->step > 255) return-1;            int index = Hashnode (child);                if (!visited[index]) {Visited[index] = true;                if (!ispass (child)) {Q.push (child);                } else {return child->step;            }} else {delete child;    }} delete node; } return-1;}        int main () {while (CIN >> n >> m) {dotperline = new unsigned char[n];         memset (Dotperline,0,n);             Onelinedot = new unsigned char*[n];              Prison = new Char*[n];                for (int i = 0; i < n; i++) {Prison[i] = new Char[m];       CIN >> Prison[i]; } for (int i = 0; i < n; i++) {Onelinedot[i] = new unsigned char[m];                  memset (ONELINEDOT[I],0,M);        } Char d[4];        Cin >> D; for (int i = 0, i < 4; i++) {switch (D[i]) {case ' N ': dirhelen[i]                [0] =-1;                DIRHELEN[I][1] = 0;            Break                Case ' S ': dirhelen[i][0] = 1;                DIRHELEN[I][1] = 0;            Break                Case ' W ': dirhelen[i][0] = 0;                DIRHELEN[I][1] =-1;            Break                Case ' E ': dirhelen[i][0] = 0;                DIRHELEN[I][1] = 1;                                                      Break              }}//find initial position node* init = new Node;        Dotcount = 0;      for (int i = 0, i < n; i++) for (int j = 0; J < m; J + +) {          if (prison[i][j] = = '. ')                    {Onelinedot[i][j] = 1;                    dotcount++;                dotperline[i]++;                    } else if (prison[i][j] = = ' H ') {init->hx = i+1;                    Init->hy = j+1;                    ONELINEDOT[I][J] = 1;                    dotcount++;                dotperline[i]++;                    } else if (prison[i][j] = = ' P ') {init->px = i+1;                    Init->py = j+1;                    ONELINEDOT[I][J] = 1;                    dotcount++;                dotperline[i]++; }} for (int i = 1; i < n-1; i++) {Dotperline[i]                       + = Dotperline[i-1];                                 for (int j = 2; J < M-1; J + +) {Onelinedot[i][j] + = onelinedot[i][j-1];     }              } int count = Dotcount*dotcount;        visited = new Bool[count];            memset (Visited,0,count);        Visited[hashnode (init)] = true;        int result = BFS (init);        if (result = =-1) cout << "Impossible" << Endl;             else cout << result << Endl;                        for (int i = 0; i < n; i++) {delete [] prison[i];        Delete [] onelinedot[i];        } Delete [] prison;        delete [] dotperline;        delete [] Onelinedot;    delete [] visited;                                  }}


Sicily 1215 out of the dungeon

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.