HDU 4127Flood-it!

Source: Internet
Author: User

HDU 4127Flood-it!

 

Flood-it! Time Limit: 2000/1000 ms (Java/Other) Memory Limit: 32768/32768 K (Java/Other) Total Submission (s): 2 Accepted Submission (s ): 1 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 (a [sub] I, j [/sub]) [sub] n × n [/sub] representing the game board. a [sub] I, j [/sub] 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

 

 

IDA *

 

How many colors are there on the map, at least the number of dyeing times required

 

Write IDA for the first time * and then get used to understanding it in practice.

 

Each time the connected block in the upper left corner is changed to a color, the minimum number of steps is used to dye the entire map into the same color.

 

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; int vis [10] [10]; int Map [10] [10]; int n; int dir [4] [2] = {0, 1, 0, -, 0,-}; bool cheak (int x, int y) {if (x> = 0 & x
     
      
= 0 & y
      
        Sum_h) return false; // pruning. The number of predetermined steps is less than the estimated number of steps for (int I = 0; I <6; I ++) {int tmp [10] [10]; memcpy (tmp, vis, sizeof (vis); if (get_cnt (I) = 0) continue; if (IDAstar (sum + 1) return true; memcpy (vis, tmp, sizeof (tmp);} return false;} int main () {while (cin> n) {if (n = 0) break; for (int I = 0; I
       
         > Map [I] [j]; memset (vis, 0, sizeof (vis); set_vis (0, 0, Map [0] [0]); sum_h = get_h (); while (! IDAstar (0) {sum_h ++;} cout <
        
         

 

 

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.