#483(div.2) B. Minesweeper

题目地址:http://codeforces.com/contest/984/problem/B

题目:

One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.

Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?

He needs your help to check it.

A Minesweeper field is a rectangle n×mn×m, where each cell is either empty, or contains a digit from 11 to 88, or a bomb. The field is valid if for each cell:

  • if there is a digit kk in the cell, then exactly kk neighboring cells have bombs.
  • if the cell is empty, then all neighboring cells have no bombs.

Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most 88 neighboring cells).

Input

The first line contains two integers nn and mm (1n,m1001≤n,m≤100) — the sizes of the field.

The next nn lines contain the description of the field. Each line contains mm characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from 11 to 88, inclusive.

Output

Print "YES", if the field is valid and "NO" otherwise.

You can choose the case (lower or upper) for each letter arbitrarily.

Examples
input
Copy
3 3
111
1*1
111
output
Copy
YES
input
Copy
2 4
*.*.
1211
output
Copy
NO
Note

In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1.

You can read more about Minesweeper in Wikipedia's article.

思路:

这道题就是验证扫雷内部的东西对不对,假如有一个*(炸弹),那么它的八连通块都得加一,如果一个格子是0则显示“.”。于是只要找到*然后用另一个数组把它周围都加一,最后两个数组比较一下就好啦。

代码:

#include<iostream>
#include<cstring>
using namespace std;
int n,m;
char maze[101][101];
int d[101][101];
int dx[8]={-1,-1,-1,0,0,1,1,1},dy[8]={-1,0,1,1,-1,-1,0,1};
void add(int x,int y)
{
    int i;
    for(i=0;i<8;i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if(nx>=0&&nx<n&&ny>=0&&ny<m&&maze[nx][ny]!='*')d[nx][ny]=d[nx][ny]+1;
    }
}
int main()
{
    while(cin>>n>>m)
    {
        memset(d,0,sizeof(d));
        int i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                cin>>maze[i][j];
            }
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(maze[i][j]=='*')add(i,j);
            }
        }
        int k=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(maze[i][j]=='.'&&d[i][j]!=0){k=1;break;}
                else if(maze[i][j]=='.')continue;
                else if(maze[i][j]=='*')continue;
                else if((int)(maze[i][j])-48!=d[i][j]){k=1;break;}
            }
        }
        if(k==0)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于 Vue.js 的简单扫雷游戏实现: HTML 代码: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue.js 扫雷游戏</title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="./minesweeper.js"></script> </head> <body> <div id="app"> <h1>Vue.js 扫雷游戏</h1> <table> <tbody> <tr v-for="(row, rowIndex) in board" :key="rowIndex"> <td v-for="(cell, colIndex) in row" :key="colIndex" @click="clickCell(rowIndex, colIndex)" :class="{ 'cell': true, 'revealed': cell.revealed, 'mine': cell.mine }"> {{ cell.content }} </td> </tr> </tbody> </table> <div v-if="gameOver"> <p>游戏结束!</p> <button @click="restart">重新开始</button> </div> <div v-else> <p>剩余雷数:{{ minesLeft }}</p> <button @click="restart">重新开始</button> </div> </div> </body> </html> ``` JavaScript 代码(minesweeper.js): ```javascript // 初始化游戏棋盘 function initBoard(row, col, numMines) { const board = []; for (let i = 0; i < row; i++) { board.push([]); for (let j = 0; j < col; j++) { board[i].push({ content: '', revealed: false, mine: false }); } } // 添加地雷 let count = 0; while (count < numMines) { const i = Math.floor(Math.random() * row); const j = Math.floor(Math.random() * col); if (!board[i][j].mine) { board[i][j].mine = true; count++; // 更新其它方格的数字 for (let r = Math.max(0, i - 1); r <= Math.min(row - 1, i + 1); r++) { for (let c = Math.max(0, j - 1); c <= Math.min(col - 1, j + 1); c++) { if (r !== i || c !== j) { board[r][c].content += 1; } } } } } return board; } // Vue.js 实例 const app = new Vue({ el: '#app', data: { row: 10, col: 10, numMines: 10, board: [], minesLeft: 0, gameOver: false }, methods: { clickCell(row, col) { const cell = this.board[row][col]; if (!cell.revealed) { cell.revealed = true; if (cell.mine) { this.gameOver = true; } else if (cell.content === '') { // 递归展开周围的空白方格 for (let r = Math.max(0, row - 1); r <= Math.min(this.row - 1, row + 1); r++) { for (let c = Math.max(0, col - 1); c <= Math.min(this.col - 1, col + 1); c++) { if (r !== row || c !== col) { this.clickCell(r, c); } } } } } // 检查游戏是否结束 this.gameOver = this.board.some(row => row.some(cell => cell.mine && cell.revealed)); }, restart() { this.board = initBoard(this.row, this.col, this.numMines); this.minesLeft = this.numMines; this.gameOver = false; this.board.forEach(row => row.forEach(cell => cell.revealed = false)); } }, created() { this.board = initBoard(this.row, this.col, this.numMines); this.minesLeft = this.numMines; }, computed: { minesLeft() { return this.numMines - this.board.reduce((count, row) => { return count + row.filter(cell => cell.revealed && cell.mine).length; }, 0); } } }); ``` 在浏览器中打开 HTML 文件,即可开始游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值