HDU 4166 & amp; BNU 32715 Robot Navigation (memory bfs)

Source: Internet
Author: User

A two-dimensional map is provided. Each point is an obstacle or an open space. A robot has three operations: 1. Move forward; 2. Turn left 90 degrees; 3. Turn right 90 degrees. Given the start point and end point, ask the number of shortest paths that reach the end point. Idea: the general question is to find the shortest length, but the question also needs to find the corresponding number. Only the minimum number of steps is recorded during the competition, but the number of steps that reach the point with the minimum number of steps is not recorded, so that they can stay in the queue ....... tie Ding tle ....... as long as the minimum number of steps reaches the point is recorded, a lot of repeated queues are reduced ..........

# Include <cstdio >#include <cstring >#include <iostream >#include <string >#include <algorithm >#include <queue> # define MAX 1111 # define INF 0x7FFFFFFF using namespace std; int n, m, mod; struct node {int x, y, dir;} st, end; queue <node> q; char map [MAX] [MAX]; int num [MAX] [MAX] [4]; // number of steps int cnt [MAX] [MAX] [4]; // how many paths reach this point in this direction int dx [] = {-,}; int dy [] = {, 0,-1}; void init () {while (! Q. empty () q. pop (); memset (num,-1, sizeof (num); memset (cnt, 0, sizeof (cnt);} int getdir (char c) {if (c = 'n') return 0; if (c = 'E') return 1; if (c = 's') return 2; if (c = 'W') return 3;} bool OK (int x, int y) {if (x> = 0 & x <n & y> = 0 & y <m & map [x] [y] = '. ') return true; return false;} void bfs () {num [st. x] [st. y] [st. dir] = 0; cnt [st. x] [st. y] [st. dir] = 1; q. push (st); while (! Q. empty () {node t = q. front (); q. pop (); node tt; // cout <t. x <<'' <t. y <''<num [t. x] [t. y] [t. dir] <endl; for (int I = 0; I <4; I ++) {// if (abs (t. dir-I) = 1 | abs (t. dir-I) = 3) {tt = t; if (num [t. x] [t. y] [I] =-1) {num [t. x] [t. y] [I] = num [t. x] [t. y] [t. dir] + 1; cnt [t. x] [t. y] [I] = cnt [t. x] [t. y] [t. dir]; tt. dir = I; q. push (tt);} else if (num [t. x] [t. y] [I] = num [t. x] [t. y] [t. dir] + 1) {Cnt [t. x] [t. y] [I] = (cnt [t. x] [t. y] [I] + cnt [t. x] [t. y] [t. dir]) % mod ;}}tt = t; tt. x + = dx [tt. dir]; tt. y + = dy [tt. dir]; if (OK (tt. x, tt. y) {if (num [tt. x] [tt. y] [tt. dir] =-1) {// move num [tt. x] [tt. y] [tt. dir] = num [t. x] [t. y] [t. dir] + 1; cnt [tt. x] [tt. y] [tt. dir] = cnt [t. x] [t. y] [t. dir]; q. push (tt);} else if (num [tt. x] [tt. y] [tt. dir] = num [t. x] [t. y] [t. dir] + 1) {cnt [tt. x] [tt. y] [tt. dir] = (cnt [tt. x] [Tt. y] [tt. dir] + cnt [t. x] [t. y] [t. dir]) % mod ;}}} void solve () {int ans = INF; for (int I = 0; I <4; I ++) {if (num [end. x] [end. y] [I]! =-1) {ans = min (ans, num [end. x] [end. y] [I]) ;}}if (ans = INF) {printf ("% d-1 \ n", mod); return ;} int sum = 0; for (int I = 0; I <4; I ++) {if (num [end. x] [end. y] [I] = ans) sum = (sum + cnt [end. x] [end. y] [I]) % mod;} printf ("% d \ n", mod, sum);} int main () {char c; int ca = 1; while (scanf ("% d", & n, & m, & mod )! = EOF) {if (mod = 0) break; init (); for (int I = 0; I <n; I ++) scanf ("% s ", map [I]); scanf ("% d", & st. x, & st. y, & end. x, & end. y); scanf ("% c", & c); st. dir = getdir (c); bfs (); printf ("Case % d:", ca ++); solve ();} return 0 ;}

 


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.