• 黑白棋(Othello, ACM/ICPC World Finals 1992, UVa220)rust解法


    你的任务是模拟黑白棋游戏的进程。黑白棋的规则为:黑白双方轮流放棋子,每次必须让新放的棋子“夹住”至少一枚对方棋子,然后把所有被新放棋子“夹住”的对方棋子替换成己方棋子。一段连续(横、竖或者斜向)的同色棋子被“夹住”的条件是两端都是对方棋子(不能是空位)。如图4-6(a)所示,白棋有6个合法操作,分别为(2,3),(3,3),(3,5),(6,2),(7,3),(7,4)。选择在(7,3)放白棋后变成如图4-6(b)所示效果(注意有竖向和斜向的共两枚黑棋变白)。注意(4,6)的黑色棋子虽然被夹住,但不是被新放的棋子夹住,因此不变白。
    在这里插入图片描述输入一个8*8的棋盘以及当前下一次操作的游戏者,处理3种指令:

    • L指令打印所有合法操作,按照从上到下,从左到右的顺序排列(没有合法操作时输出No legal move)。
    • Mrc指令放一枚棋子在(r,c)。如果当前游戏者没有合法操作,则是先切换游戏者再操作。输入保证这个操作是合法的。输出操作完毕后黑白方的棋子总数。
    • Q指令退出游戏,并打印当前棋盘(格式同输入)。

    样例:
    输入

    --------
    --------
    --------
    ---WB---
    ---BW---
    --------
    --------
    --------
    W
    L
    M35
    L
    Q
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    输出

    [(3, 5), (4, 6), (5, 3), (6, 4)]
    W is 4. B is 1
    [(3, 4), (3, 6), (5, 6)]
    --------
    --------
    ----W---
    ---WW---
    ---BW---
    --------
    --------
    --------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    解法:

    use std::io;
    
    enum Cmd {
        Print,
        Move(usize, usize),
        Quit,
    }
    fn main() {
        let mut grid: Vec<Vec<char>> = vec![];
        for _i in 0..8 {
            let mut buf = String::new();
            io::stdin().read_line(&mut buf).unwrap();
            grid.push(buf.trim().chars().collect());
        }
        let mut buf = String::new();
        io::stdin().read_line(&mut buf).unwrap();
        let mut curplayer = buf.trim().chars().nth(0).unwrap();
        let mut cmds: Vec<Cmd> = vec![];
        loop {
            let mut buf = String::new();
            io::stdin().read_line(&mut buf).unwrap();
            buf = buf.trim().to_string();
            if buf == "L" {
                cmds.push(Cmd::Print);
            } else if buf == "Q" {
                cmds.push(Cmd::Quit);
                break;
            } else {
                let i = buf.chars().nth(1).unwrap().to_digit(10).unwrap();
                let j = buf.chars().nth(2).unwrap().to_digit(10).unwrap();
                cmds.push(Cmd::Move(i as usize, j as usize));
            }
        }
        for i in cmds {
            match i {
                Cmd::Print => printmoves(&grid, curplayer),
                Cmd::Move(x, y) => fangzi(&mut grid, &mut curplayer, (x, y)),
                Cmd::Quit => {
                    printgrid(&grid);
                    break;
                }
            }
        }
    }
    
    fn fangzi(grid: &mut Vec<Vec<char>>, curp: &mut char, pos: (usize, usize)) {
        let allmoves = getmoves(grid, *curp);
        if allmoves.is_empty() {
            *curp = oposite(*curp);
        }
        let newpos = (pos.0 - 1, pos.1 - 1);
        grid[newpos.0][newpos.1] = *curp;
        let runs = [
            (0, -1),
            (0, 1),
            (-1, 0),
            (1, 0),
            (-1, -1),
            (1, 1),
            (-1, 1),
            (1, -1),
        ];
        for d in runs {
            if judge(grid, *curp, newpos, d) {
                change(grid, *curp, newpos, d);
            }
        }
        let nums = getnums(grid);
        println!("W is {}. B is {}", nums.0, nums.1);
        *curp = oposite(*curp);
    }
    
    fn getnums(grid: &Vec<Vec<char>>) -> (u32, u32) {
        let mut nums = (0, 0);
        for i in 0..8 {
            for j in 0..8 {
                if grid[i][j] == 'W' {
                    nums.0 += 1;
                } else if grid[i][j] == 'B' {
                    nums.1 += 1;
                }
            }
        }
        return nums;
    }
    fn oposite(p: char) -> char {
        if p == 'W' {
            'B'
        } else {
            'W'
        }
    }
    fn printgrid(grid: &Vec<Vec<char>>) {
        for line in grid.iter() {
            println!("{}", line.iter().collect::<String>());
        }
    }
    fn getmoves(grid: &Vec<Vec<char>>, curp: char) -> Vec<(usize, usize)> {
        let mut allmoves: Vec<(usize, usize)> = vec![];
        let runs = [
            (0, -1),
            (0, 1),
            (-1, 0),
            (1, 0),
            (-1, -1),
            (1, 1),
            (-1, 1),
            (1, -1),
        ];
        for i in 0..8 {
            for j in 0..8 {
                //println!("i,j: {},{}", i, j);
                if grid[i][j] != '-' {
                    continue;
                }
    
                for d in runs {//检查八个方向
                    if judge(grid, curp, (i, j), d) {
                        allmoves.push((i + 1, j + 1));
                        break;
                    }
                }
            }
        }
        return allmoves;
    }
    fn judge(grid: &Vec<Vec<char>>, curp: char, pos: (usize, usize), run: (i32, i32)) -> bool {
        let mut x = pos.0;
        let mut y = pos.1;
        let mut bjiazhu = false;
        while x > 0 && x < 7 && y > 0 && y < 7 {
            x = (x as i32 + run.0) as usize;
            y = (y as i32 + run.1) as usize;
            if grid[x][y] == '-'{
                break;
            }
            if grid[x][y] == oposite(curp) {
                bjiazhu = true;
            }else if bjiazhu {
                return true;
            }else {
                break;
            }
        }
        return false;
    }
    fn change(grid: &mut Vec<Vec<char>>, curp: char, pos: (usize, usize), run: (i32, i32)) {
        let mut x = pos.0;
        let mut y = pos.1;
        while x > 0 && x < 7 && y > 0 && y < 7 {
            x = (x as i32 + run.0) as usize;
            y = (y as i32 + run.1) as usize;
            if grid[x][y] == oposite(curp) {
                grid[x][y] = curp;
            } else {
                return;
            }
        }
    }
    fn printmoves(grid: &Vec<Vec<char>>, curp: char) {
        let allmoves = getmoves(grid, curp);
        if allmoves.is_empty() {
            println!("No legal move");
        } else {
            println!("{:?}", allmoves);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
  • 相关阅读:
    PSO-DBSCAN聚类
    css如何讲一个框分为三份写内容?
    curl常用参数详解及示例
    Tomcat内存马学习5:Agent型
    VSCODE 系列(一)VSCODE下载和安装
    freeswitch拨打分机号
    懒羊羊闲话4 - 献给那些苦于学习无法入门的同学
    aiohttp从入门到精通
    QT 线程学习
    protobufjs实现protobuf序列化与反序列化
  • 原文地址:https://blog.csdn.net/inxunxun/article/details/133916996