Sha 10798-Be wary of Roses (memory-based BFS)

Source: Internet
Author: User

Sha 10798-Be wary of Roses (memory-based BFS)
Ultraviolet A 10798-Be wary of Roses

Question Link

Given a map, people start at the center and choose a method to go out to minimize the maximum value of the flowers they step in any direction.

Train of Thought: Use the priority queue for BFS. When the minimum step is taken out each time, the status of the extensive search record is the current position, and the number of flowers in four directions.

Code:

#include 
 
  #include 
  
   #include #include 
   
    using namespace std;const int N = 21;const int d[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};int n, vis[N][N][11][11][11][11];char g[N][N];struct State {int x, y, val;int up, left, down, right;State() {x = y = up = left = down = right = 0;}State(int x, int y, int up, int left, int down, int right) {this->x = x;this->y = y;this->up = up;this->left = left;this->down = down;this->right = right;val = max(max(max(up,left), down), right);}bool operator < (const State& c) const {return val > c.val;}} s;void init() {for (int i = 0; i < n; i++) {scanf("%s", g[i]);for (int j = 0; j < n; j++)if (g[i][j] == 'P')s.x = i, s.y = j;}}int bfs() {memset(vis, 0, sizeof(vis));priority_queue
    
      Q;Q.push(s);vis[s.x][s.y][0][0][0][0] = 1;while (!Q.empty()) {State u = Q.top();Q.pop();if (u.x == 0 || u.x == n - 1 || u.y == 0 || u.y == n - 1) return u.val;for (int i = 0; i < 4; i++) {int xx = u.x + d[i][0];int yy = u.y + d[i][1];int up = u.up;int left = u.left;int down = u.down;int right = u.right;if (g[xx][yy] == 'R') up++;if (g[n - 1 - yy][xx] == 'R') left++;if (g[n - 1 - xx][n - 1 - yy] == 'R') down++;if (g[yy][n - 1 - xx] == 'R') right++;if (!vis[xx][yy][up][left][down][right]) {vis[xx][yy][up][left][down][right] = 1;Q.push(State(xx, yy, up, left, down, right));}}}}int main() {while (~scanf("%d", &n) && n) {init();printf("At most %d rose(s) trampled.\n", bfs());}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.