zoukankan      html  css  js  c++  java
  • poj 2965 The Pilots Brothers' refrigerator

    这题和前一题解题思路一样,但就是不知道为什么,一直超时,原因在于那个if(judge())的判断,改成flag=judge()就不超了,很神奇,搞不懂为什么。

    The Pilots Brothers' refrigerator
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15409   Accepted: 5779   Special Judge

    Description

    The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

    There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

    The task is to determine the minimum number of handle switching necessary to open the refrigerator.

    Input

    The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

    Output

    The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

    Sample Input

    -+--
    ----
    ----
    -+--

    Sample Output

    6
    1 1
    1 3
    1 4
    4 1
    4 3
    4 4

    Source

      1 #include<stdio.h>
      2 #include<string.h>
      3 char map[5][5];
      4 int vis[5][5];
      5 int x[100000],y[100000];
      6 int flag ,step,n;
      7 int judge()
      8 {
      9      int i,j;
     10      for(i=1;i<=4;i++)
     11      for(j=1;j<=4;j++)
     12      if(vis[i][j]!=0)
     13      return 0;
     14      return 1;
     15 }
     16 void turn(int i,int j)
     17 {
     18      vis[i][j]=!vis[i][j];
     19      int t;
     20      for(t=1;t<=4;t++)
     21      {
     22           vis[t][j]=!vis[t][j];
     23           vis[i][t]=!vis[i][t];
     24      }
     25 
     26      return ;
     27 }
     28 void dfs(int i,int j,int step1)
     29 {
     30      if(step==step1)
     31      {
     32        flag=judge();
     33        return ;
     34      }
     35      if(step1>16)
     36      return ;
     37      if(i==5||flag==1)
     38      return;
     39      turn(i,j);
     40      if(j<4)
     41      {
     42           x[step1]=i;
     43           y[step1]=j;
     44           dfs(i,j+1,step1+1);
     45      }
     46 
     47        else
     48        {
     49              x[step1]=i;
     50              y[step1]=j;
     51             dfs(i+1,1,step1+1);
     52        }
     53 
     54        turn(i,j);
     55         if(j<4)
     56        dfs(i,j+1,step1);
     57        else
     58        dfs(i+1,1,step1);
     59 return ;
     60 }
     61 int main()
     62 {
     63      int i,j;
     64           flag=0;
     65 
     66           for(i=1;i<5;i++)
     67           {
     68                for(j=1;j<5;j++)
     69                scanf("%c",&map[i][j]);
     70              getchar();
     71           }
     72 
     73           for(i=1;i<5;i++)
     74           {
     75                for(j=1;j<5;j++)
     76             {
     77                  if(map[i][j]=='-')
     78                  vis[i][j]=0;
     79                  else
     80                  vis[i][j]=1;
     81             }
     82 
     83           }
     84 
     85             for(step=0;step<=16;step++)
     86             {
     87 
     88 
     89                  dfs(1,1,0);
     90                  if(flag)
     91                  break;
     92             }
     93           if(flag)
     94           {
     95                printf("%d
    ",step);
     96                for(i=0;i<step;i++)
     97                printf("%d %d
    ",x[i],y[i]);
     98           }
     99 
    100      return 0;
    101 }
    View Code
  • 相关阅读:
    AOP面向切面编程
    java中与运算,或运算,异或运算,取反运算
    Java中replace和replaceall的区别
    Satisfying memory ordering requirements between partial reads and non-snoop accesses
    JS判断字符串中是否存在某个字符
    PHP 简介
    PHP 是什么
    mysql substr() 函数
    MySQL 数学函数
    MySQL 字符串截取SUBSTRING()函数
  • 原文地址:https://www.cnblogs.com/llei1573/p/3204983.html
Copyright © 2011-2022 走看看