Pass the note (a)

Source: Internet
Author: User

this oneThe question and the Zhengzhou light industry of the school race of the question is the same, then on the search, this problem and try to try the idea is to start from the lower right corner to the upper left corner of the search, find the highest kind of the road and then the route to zero, and then to search once again, in the future two times the kindness is the final The results, and then found that this is not the optimal solution, this two-way search is a greedy thought greed is only a probability of the optimal solution, attached below, the above thought code, and can see the non-optimal solution of the data
1#include <stdio.h>2#include <string.h>3#include <math.h>4#include <iostream>5#include <algorithm>6#include <queue>7#include <vector>8#include <Set>9#include <stack>Ten#include <string> One#include <sstream> A#include <map> -#include <cctype> -#include <limits.h> the using namespacestd; - intn,m,a[ -][ -],visited[ -][ -],b[2][2]={-1,0,0,-1},result,flag,mark; - structnode - { +     intX,y,step; -FriendBOOL operator<(node S1,node s2) +     { A         returns1.step<S2.step; at     } - }; -Priority_queue<node>Q; - voidBFS (intYintx) - { -Node q={x,y,a[y][x]}; invisited[y][x]=1; - Q.push (Q); to      while(!q.empty ()) +     { -Node e=q.top (); the Q.pop (); *          for(intI=0;i<2; i++) $         {Panax Notoginsengq.x=e.x+b[i][0],q.y=e.y+b[i][1]; -             if(q.x>=0&&q.x<m&&q.y>=0&&q.y<n&&!visited[q.y][q.x]) the             { +visited[q.y][q.x]=1; Aq.step=e.step+a[q.y][q.x]; the Q.push (Q); +                 if(q.x==0&&q.y==0) -                 { $result+=Q.step; $flag=1; -                      Break; -                 } the             } -         }Wuyi         if(flag) the         { -              while(!q.empty ()) Wu Q.pop (); -         } About     } $ } - voidDFS (intYintXintStep) - { -     if(step==result| |Mark) A     { +a[y][x]=1; the         return; -mark=1; $     } theDFS (Y-1, x,step+a[y-1][x]); the     if(step==result| |Mark) the     { thea[y][x]=1; -         return; inmark=1; the     } theDFS (y,x-1, step+a[y][x-1]); About     if(step==result| |Mark) the     { thea[y][x]=1; the         return; +mark=1; -     } the }Bayi intMain () the { the     intT; -scanf"%d",&t); -      while(t--) the     { thescanf"%d%d",&n,&m); the          for(intI=0; i<n;i++) the              for(intj=0; j<m;j++) -scanf"%d",&a[i][j]); theMemset (visited,0,sizeof(visited)); themark=result=flag=0; theBFS (n1, M-1);94DFS (N-1, M-1,0); theprintf"%d\n", result); the     } the     return 0;98}

Pass the note (a)

Related Article

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.