Hdu Flood-it! (IDA * algorithm)

Source: Internet
Author: User

Hdu Flood-it! (IDA * algorithm)

 

 

Flood-it! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 1703 Accepted Submission (s): 396

Problem Description Flood-it is a fascinating puzzle game on Google + platform. The game interface is like follows:

At the beginning of the game, system will randomly generate an N × N square board and each grid of the board is painted by one of the six colors. the player starts from the top left corner. at each step, he/she selects a color and changes all the grids connected with the top left corner to that specific color. the statement "two grids are connected" means that there is a path between the certain two grids under condition that each pair of adjacent grids on this path is in the same color and shares an edge. in this way the player can flood areas of the board from the starting grid (top left corner) until all of the grids are in same color. the following figure shows the earliest steps of a 4 × 4 game (colors are labeled in 0 to 5 ):

Given a colored board at very beginning, please find the minimal number of steps to win the game (to change all the grids into a same color ).


Input The input contains no more than 20 test cases. For each test case, the first line contains a single integer N (2 <= N <= 8) indicating the size of game board.

The following N lines show an N × N matrix (ai, j) n × n representing the game board. ai, j is in the range of 0 to 5 representing the color of the corresponding grid.
The input ends with N = 0.

Output For each test case, output a single integer representing the minimal number of steps to win the game.

Sample Input
20 0 0 030 1 21 1 22 2 10

Sample Output
03

Source 2011 Asia Fuzhou Regional Contest
Recommend lcy | We have carefully selected several similar problems for you: 4123 4122 4124 4125
The square of n * n can only be dyed from the upper left corner and connected to the same number each time. After the change, the square will be merged and changed to any other color. The minimum number of steps to be output can be changed to the same number. Note: 1. Each change can only be changed to a number in the upper left corner. Or you can change it after merging it with the upper left corner. 2. The number of statuses is large. It is obvious that bfs will be used for the first IDA * search question. First, Mark vis as 1 in a connected block in the upper left corner. Second, the color of the connected block is different from that of the connected block, but the adjacent one is marked as 2. The color can be changed next time. 3. valuation function, used to calculate the optimal value in the ideal state.
For details, see the code.
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; int dir [4] [2] = {,-, 0,-1}; int n; int a [10] [10], vis [10] [10], want, vv [10] [10]; int kk = 0; void checkcolor (int x, int y, int color) {// cout <11111 <
     
      
= N | Y <0 | Y> = n | vv [X] [Y] = 1) {continue ;} if (a [X] [Y] = color | vis [X] [Y] = 1) // if the color is the same, Mark Vis as 1 {vv [X] [Y] = vis [X] [Y] = 1; // mark as checkcolor (X, y, color); // continue searching until there is no same color} else vis [X] [Y] = 2; // to different colors, directly mark as adjacent} int get_cnt (int color) // connect squares of the same color {int t = 0; for (int I = 0; I
      
        Want) // if the number of steps I have taken + the best value in the evaluate function> what I want, return directly. Return false; if (dep = want) // if you find the one that I have already taken is equal to what I want, return true; for (int I = 0; I <6; I ++) {int tmp [10] [10]; memcpy (tmp, vis, sizeof (vis); if (get_cnt (I )) // determine whether the current color can be connected and merge it into a block in the upper left corner {memset (vv, 0, sizeof (vv); checkcolor (, I ); if (IDA (dep + 1) {return true ;}} memcpy (vis, tmp, sizeof (vis) ;}return false ;} int main () {while (~ Scanf (% d, & n) {if (n = 0) break; for (int I = 0; I
       
        

 

 

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.