zoukankan      html  css  js  c++  java
  • 18.05.28 广搜作业

    A:Flip Game

    描述

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

    1. Choose any one of the 16 pieces. 
    2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).


    Consider the following position as an example:

    bwbw
    wwww
    bbwb
    bwwb
    Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

    bwbw
    bwww
    wwwb
    wwwb
    The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.输入The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.输出Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).

    样例输入

    bwwb
    bbwb
    bwwb
    bwww

    样例输出

    4
    

    来源Northeastern Europe 2000

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string.h>
     4 #include <math.h>
     5 #include <algorithm>
     6 #include <stdlib.h>
     7 #include <queue>
     8 
     9 using namespace std;
    10 
    11 struct chess {
    12     bool map[17];//black:1 white:0
    13     int step=0;
    14     int x=0, y=0;
    15 };
    16 
    17 queue<chess> all;
    18 chess initial;
    19 int steps,flag=0;
    20 int tosingle[5][5] = { 0,0,0,0,0,
    21                     0,1,2,3,4,
    22                     0,5,6,7,8,
    23                     0,9,10,11,12,
    24                     0,13,14,15,16 };
    25 int singletox[17] = { 0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4 };
    26 int singletoy[17] = { 0,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4 };
    27 int dir1[4] = { 0,0,1,-1 }, dir2[4] = { 1,-1,0,0 };
    28 
    29 bool complete(chess&a) {
    30     int sum = 0;
    31     for (int i = 1; i <= 16; i++)
    32         sum += a.map[i];
    33     if (sum == 16 || sum == 0)
    34         return true;
    35     return false;
    36 }
    37 
    38 void bfs()//last time which chess we flipped  last is which step
    39 {
    40     while (!all.empty()) {
    41         chess now = all.front();
    42         all.pop();
    43         if (complete(now) == true) {
    44             printf("%d
    ", now.step);
    45             flag = 1;
    46             return;
    47         }
    48         for (int i = tosingle[now.x][now.y]+1; i <=16; i++) {
    49             int xx = singletox[i], yy = singletoy[i];
    50             chess afterchange(now);
    51             afterchange.step = now.step + 1;
    52             afterchange.x = xx, afterchange.y = yy;
    53             afterchange.map[i] ^= 1;
    54             for (int j = 0; j < 4; j++)
    55             {
    56                 int _x = xx + dir1[j], _y = yy + dir2[j];
    57                 if ((_x <= 4 && _x >= 1) && (_y <= 4 && _y >= 1))
    58                     afterchange.map[tosingle[_x][_y]] ^= 1;
    59             }
    60             all.push(afterchange);
    61         }
    62     }
    63     if (flag == 0)
    64         printf("Impossible
    ");
    65 }
    66 
    67 void init() {
    68     for(int i=1;i<=4;i++)
    69         for (int j = 1; j <= 4; j++) {
    70             char ch;
    71             cin>>ch;
    72             if (ch == 'b')
    73                 initial.map[tosingle[i][j]] = 1;
    74         }
    75     all.push(initial);
    76     bfs();
    77 }
    78 
    79 int main()
    80 {
    81     init();
    82     return 0;
    83 }
    View Code

    实现得不太好

    B:迷宫问题

    描述

    定义一个二维数组: 

    int maze[5][5] = {
    
    0, 1, 0, 0, 0,
    
    0, 1, 0, 1, 0,
    
    0, 0, 0, 0, 0,
    
    0, 1, 1, 1, 0,
    
    0, 0, 0, 1, 0,
    
    };


    它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

     

    输入

    一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。输出左上角到右下角的最短路径,格式如样例所示。

    样例输入

    0 1 0 0 0
    0 1 0 1 0
    0 0 0 0 0
    0 1 1 1 0
    0 0 0 1 0

    样例输出

    (0, 0)
    (1, 0)
    (2, 0)
    (2, 1)
    (2, 2)
    (2, 3)
    (2, 4)
    (3, 4)
    (4, 4)
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string.h>
     4 #include <math.h>
     5 #include <algorithm>
     6 #include <stdlib.h>
     7 #include <memory.h>
     8 #include <queue>
     9 
    10 using namespace std;
    11 
    12 struct point {
    13     int x, y;
    14     point(int _x, int _y) :x(_x), y(_y) { }
    15     point() {}
    16 };
    17 queue<point> all;
    18 int map[7][7];//the map of maze
    19 int dir1[4] = { 0,0,1,-1 }, dir2[4] = { 1,-1,0,0 };
    20 bool visited[7][7];
    21 point last[7][7];
    22 
    23 
    24 void bfs()
    25 {
    26     while (!all.empty()) {
    27         point now = all.front();
    28         all.pop();
    29         for (int i = 0; i < 4; i++) {
    30             int _x = now.x + dir1[i], _y = now.y + dir2[i];
    31             if (!visited[_x][_y] && !map[_x][_y]) {
    32                 point newnode(_x, _y);
    33                 last[_x][_y] = now;
    34                 visited[_x][_y] = 1;
    35                 if (_x == 1 && _y == 1) {
    36                     printf("(%d, %d)
    ", newnode.x-1, newnode.y-1);
    37                     while (_x!= 5 || _y!= 5) {
    38                         int xx = _x, yy = _y;
    39                         _x = last[xx][yy].x, _y = last[xx][yy].y;
    40                         printf("(%d, %d)
    ", _x-1, _y-1);
    41                     }
    42                     return;
    43                 }
    44                 all.push(newnode);
    45             }
    46         }
    47     }
    48 }
    49 
    50 void init() {
    51     for (int i = 0; i < 7; i++)
    52         for (int j = 0; j < 7; j++)
    53             map[i][j] = 1;
    54     for(int i=1;i<=5;i++)
    55         for (int j = 1; j <= 5; j++) 
    56             scanf("%d", &map[i][j]);
    57     visited[5][5] = 1;
    58     all.push(point(5, 5));
    59     bfs();
    60 }
    61 
    62 int main()
    63 {
    64     init();
    65     return 0;
    66 }
    View Code

    广搜突然简单……令人害怕

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    几个.net开源项目(转)
    UILabel显示换行的方法
    使用TDBXReader或TDataSet回传数据,中文乱码问题解决办法
    NSDateFormatter setDateFormat 自定义日期时间格式
    iPhone开发之打包zip文件
    创建log文件的代码
    去除iphone图标的半弧高亮效果
    Ajax XMLHttpRequest对象open方法的参数 为什么要把时间戳追加到目标URL?
    时间校验
    WPF视频
  • 原文地址:https://www.cnblogs.com/yalphait/p/9099641.html
Copyright © 2011-2022 走看看