Card game _ hdu_2209 (two-way wide search). java

Source: Internet
Author: User

Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 1885 Accepted Submission (s ): 661 Problem Description There is a card game that is very interesting. It gives you N cards in one row. Cards have both sides, and the cards at the beginning may be in a messy state (somewhat positive, and now you need to sort these cards. But the trouble is that every time you flip a card (from the front to the back, or from the back to the front), two cards (the leftmost and rightmost cards, it will only affect the nearby one. You must also flip it. Now, I will give you a messy state and ask if you can sort them out so that each card is facing up. If you can, the minimum number of operations required. Input has multiple cases. Each case is Input with A 01 sign string (length not greater than 20). 1 indicates the opposite side facing up, and 0 indicates the front facing up. For each group of cases, If you can flip the Output, the Output requires at least the number of flip operations; otherwise, the Output is NO. Sample Input01011 Sample OutputNO1 Authorwangye Recommendwangye | We have carefully selected several similar problems for you: 2102 1495 1180 2201 2200 [java] view plaincopyimport java. io. bufferedReader; import java. io. inputStreamReader; import java. util. hashMap; import java. util. using list; import java. util. queue; public class Main {// timeout static String sf = ""; static int len = 0; // static HashMap <Str IngBuffer, Integer> map; static boolean OK = false; public static void main (String [] args) {BufferedReader bf = new BufferedReader (new InputStreamReader (System. in); String s; try {while (s = bf. readLine ())! = Null) {OK = false; StringBuffer ss = new StringBuffer (s); len = ss. length (); sf = ""; for (int I = 0; I <len; I ++) sf + = "0"; HashMap <String, integer> map = new HashMap <String, Integer> (); map. put (s, 0); P p = new P (); p.s = ss; p. num = 0; Queue <P> q = new Queue list <P> (); q. add (p); bfs (q, map); if (! OK) System. out. println ("No") ;}} catch (Exception e) {e. printStackTrace () ;}} private static void bfs (Queue <P> q, HashMap <String, Integer> map) {while (! Q. isEmpty () {boolean okk = false; if (okk) {OK = true; return;} P d = new P (); d = q. poll (); String k = d. s. toString (); if (k. equals (sf) {System. out. println (d. num); OK = true; return;} StringBuffer g = d. s; for (int I = 0; I <len; I ++) {StringBuffer f = new StringBuffer (g); if (f. charAt (I) = '0') {f. deleteCharAt (I); f. insert (I, 1);} else {f. deleteCharAt (I); f. insert (I, 0);} if (I> 0) {if (f. charAt (I-1) = '0') {f. deleteCha RAt (I-1); f. insert (I-1, 1);} else {f. deleteCharAt (I-1); f. insert (I-1, 0) ;}}if (I + 1 <len) {if (f. charAt (I + 1) = '0') {f. deleteCharAt (I + 1); f. insert (I + 1, 1);} else {f. deleteCharAt (I + 1); f. insert (I + 1, 0) ;}} String f1 = f. toString (); if (map. get (f1) = null) {// System. out. println ("*****" + f); // System. out. println (map. get (f); if (f1.equals (sf) {System. out. println (d. num + 1); okk = true; OK = true; return;} P H = new P (); h. s = f; h. num = d. num + 1; map. put (f1, 0); q. add (h) ;}}/* if (f. charAt (d. i) = '0') {f. deleteCharAt (d. i); f. insert (d. i, 1);} else {f. deleteCharAt (d. i); f. insert (d. i, 0);} if (d. i> 0) {if (f. charAt (d. i-1) = '0') {f. deleteCharAt (d. i-1); f. insert (d. i-1, 1);} else {f. deleteCharAt (d. i-1); f. insert (d. i-1, 0) ;}} if (d. I + 1 <len) {if (f. charAt (d. I + 1) = '0') {f. deleteCharAt (d. I + 1); f. insert (d. I + 1, 1);} el Se {f. deleteCharAt (d. I + 1); f. insert (d. I + 1, 0) ;}} if (d. I + 1! = Len) {P g = new P (); g. s = f; g. I = d. I + 1; g. num = d. num + 1;} */} class P {StringBuffer s = null; int num = 0 ;}

/*  * 01543093 2013-11-10 12:19:26 Accepted    1004    5406 MS 26468 KB    Java    zhangyi  9547285    2013-11-10 14:27:34 Accepted    2209    5593MS  26476K  1594 B  Java    zhangyi  */  import java.io.BufferedReader;  import java.io.InputStreamReader;  import java.util.LinkedList;  import java.util.Queue;    public class Main {//AC      static char str[] = new char[30];      static int dis[] = new int[1 << 20], vis[] = new int[1 << 20],              base[] = new int[20], len, aim;        public static void main(String[] args) {          BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));          int i;          aim = 0;          String s = null;          try {              while ((s = bf.readLine()) != null) {                  str = s.toCharArray();                  len = s.length();                  base[0] = 3;                  base[len - 1] = 3 << (len - 2);                  for (i = 1; i < len - 1; i++)                      base[i] = 7 << (i - 1);                  if (len == 1) {                      if (str[0] == '1')                          System.out.println(1);                      else                          System.out.println(0);                      continue;                  }                  int ans = bfs();                  if (ans == -1)                      System.out.println("NO");                  else                      System.out.println(ans);              }          } catch (Exception e) {              e.printStackTrace();          }      }        public static int bfs() {          int i, limit = 1 << len;          Integer now, next;          Queue<Integer> q = new LinkedList<Integer>();            now = 0;          for (i = len - 1; i >= 0; i--)              now = now * 2 + str[i] - '0';          for (i = 0; i < limit; i++)              vis[i] = 0;          dis[now] = 0;          vis[now] = 1;          q.add(now);            while (!q.isEmpty()) {              now = q.poll();              if (now == aim)                  return dis[aim];              for (i = 0; i < len; i++) {                  next = now ^ base[i];                  if (vis[next] != 0)                      continue;                  dis[next] = dis[now] + 1;                  vis[next] = 1;                  q.add(next);              }          }            return -1;      }  }  

 


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.