Noj--1559jump to the Top of Mountain (simple brute force dfs+ slag code)

Source: Internet
Author: User

  • [1559] Jump to the Top of Mountain
  • Time limit: Ms Memory limit: 65535 K
  • Problem description
  • Have you played a game named Minecraft?
    In the game, there are a thing alloy you can not destroy, and their shape is a 1*1*1 area cube. I can only jump 1 meter at vertical height, it means I can jump from x meters height to y meters height when y isn't more than X+1.

    Now, there are a rectangle field of m*n areas, each area is a positive interger x, x means the number of alloy . You can have only a jump to four adjacent areas (up, down, left and right).

    At the beginning, the Rectangle field, and the height is 0 out of the rectangle field.
    Can help me? I only want to know if I can jump to the peak of field?

  • Input
  • Input ends of EOF.
    First line contains positive integers m and n (3 <= m, n <= 100).
    Then M. Lines, each line contains n intergers x (0 <= x <= 10000).
  • Output
  • If I can jump to the peak, output "YES", or Output "NO".
  • Sample input
  • 5 52 2 1 2 22 2 2 2 22 2 3 2 22 2 2 2 22 2 2 2 23 32 1 22 0 11 1 34 41 1 1 11 3 1 21 1 1 31 1 1 44 41 2 3 48 7 6 59 10 11 1216 15 14 13
  • Sample output
  • Yesnoyesyes

Basically did not write a few search. Because from the edge into the enumeration edge of all the lattice to search, if carefully read the question must be able to know to go low ... Thought only a flat or a tall lattice also rewarded a WA. The egg hurts.

Code very rubbing + slag:

#include <iostream> #include <algorithm> #include <cstdlib> #include <sstream> #include < cstring> #include <cstdio> #include <string> #include <deque> #include <stack> #include < cmath> #include <queue> #include <set> #include <map>using namespace std;typedef long Long ll;#  Define INF 0x3f3f3f3fint pos[110][110];int vis[110][110];int flag=0;int tall;int n,m;void Look (const int &x,const int &y,const int &pre) {if (x<0| | y<0| | x>=n| | y>=m| | vis[x][y]==1)//boundary + whether to access the return;vis[x][y]=1;//tag access if (pos[x][y]-pre<=1) {if (Pos[x][y]==tall) {flag=1;puts ("YES "); return;} Look (X+1,y,pos[x][y]), if (!flag) look (X-1,y,pos[x][y]), if (!flag) look (X,y+1,pos[x][y]), if (!flag) look (X,y-1,pos[x] [y]);} else{vis[x][y]=0;//Road is not accessible, not counted. return;}} int main (void) {int i,j;while (~scanf ("%d%d", &n,&m)) {memset (pos,0,sizeof (POS)); tall=-1;flag=0;for (i=0; i <n; i++) {for (j=0; j<m; J + +) {scanf ("%d", &pos[i][j]); Tall=max (Tall,pos[i][j]);}} if (tall==0) {Puts ("NO"); continue;} for (j=0; j<m; J + +) {if (pos[0][j]==1) {memset (vis,0,sizeof (VIS)); look (0,j,0);} if (flag) break;} if (!flag) {for (j=0; j<m; J + +) {if (pos[n-1][j]==1) {memset (vis,0,sizeof (VIS)); look (n-1,j,0);} if (flag) break;}} if (!flag) {for (i=0; i<n; i++) {if (pos[i][0]==1) {memset (vis,0,sizeof (VIS)), look (i,0,0);} if (flag) break;}} if (!flag) {for (i=0; i<n; i++) {if (pos[i][m-1]==1) {memset (vis,0,sizeof (VIS)), look (i,m-1,0);} if (flag) break;}} if (!flag) puts ("NO");} return 0;}

Noj--1559jump to the Top of Mountain (simple brute force dfs+ slag code)

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.