日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Nowcoder] 2021年度训练联盟热身训练赛第六场 Mini Battleship | 深搜 回溯 乱搞

發布時間:2023/12/16 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Nowcoder] 2021年度训练联盟热身训练赛第六场 Mini Battleship | 深搜 回溯 乱搞 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

題目描述

Battleship is a game played by two players. Each player has their own grid, which is hidden from their opponent. Each player secretly places some ships on their grid. Each ship covers a horizontal or vertical straight line of one or more continguous squares. Ships cannot overlap. All ships are considered distinct, even if they have the same size.
After placing their ships, the players then take turns taking shots at their opponent’s ships by calling out a coordinate of their opponent’s grid. The opponent must honestly say whether the shot was a hit or a miss. When all of a ship’s squares are hit, that ship sinks (“You sunk my battleship!!”). A player loses when all of their ships are sunk.
Bob is playing a game of Mini Battleship against Alice. Regular Battleship is played on a 10×10 grid with 5 ships. Mini Battleship is much smaller, with a grid no larger than 5×5 and possibly fewer than 5 ships. Bob wonders how many ship placements are possible on Alice’s board given what he knows so far.
The answer will be 0 if Alice is cheating! (Or, if the game setup isn’t possible.)

輸入

The first line of input contains two space-separated integers n (1 ≤ n ≤ 5) and k (1 ≤ k ≤ 5),which represent a game of Mini Battleship played on an n×n grid with k ships.
Each of the next n lines contains a string s (|s| = n). This is what Bob sees of Alice’s grid so far.
A character ‘X’ represents one of Bob’s shots that missed. A character ‘O’ (Letter O, not zero) represents one of Bob’s shots that hit. A Dot (‘.’) represents a square where Bob has not yet taken a shot. Each of the next k lines contains a single integer x (1 ≤ x ≤ n). These are the sizes of the ships.

輸出

Output a single integer, which is the number of ways the k distinct ships could be placed on Alice’s grid and be consistent with what Bob sees.

樣例輸入 Copy

【樣例1】

4 3 .... .OX. .... O..X 3 2 1

【樣例2】

4 4 .X.X .XX. ...X .... 1 2 3 4

【樣例3】

2 2 .. .. 2 2

樣例輸出 Copy

【樣例1】

132

【樣例2】

6

【樣例3】

4

題意:
給定一個n?nn * nn?n的矩陣,其中在XXX上一定不可以放置船,而在OOO上面一定要放置船,在′.′'.'.上面可以放置船,也可以不放,問將以下mmm艘船,大小均為1?x1 * x1?x,放置在矩陣中的方案數量
思路:
類似經典的八皇后問題,
首先將所有的m個都成功放置之后,并且所有的O均已成功放置船艘,此時的方案書就應該 + 1
注意船的形狀一共有兩種情況:橫著和豎著,但是對于1 * 1的情況來說就只有一種狀態,這里要特判掉
我們用judge()judge()judge()函數來判斷是否能夠是否可以放置該船,如果說要是放置過程中遇到了’X’,那是一定不能夠放下去的,反之可以放下這艘船,對于能夠放下去的船,我們將它的位置vis[i][j] += 1;

char s[7][7]; int in[10]; int n, m; int ans; int vis[7][7]; bool ok() {for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {if(s[i][j] == 'O' && vis[i][j] == 0)return false;/// O must place but not vis}}return true; } bool judge(int pos, int x, int y, int t) {if(t == 0) {if(y + in[pos] - 1 > n)return false;/// <= nfor(int i = y; i < y + in[pos]; i++) {if(vis[x][i] || s[x][i] == 'X')return false;/// X not place}} else {if(x + in[pos] - 1 > n)return false;for(int i = x; i < x + in[pos]; i++) {if(vis[i][y] || s[i][y] == 'X')return false;}}return true; } void addBattleship(int x, int y, int pos, int id, int val) {if(id == 0) {for(int i = y; i < y + in[pos]; i++) {vis[x][i] += val;}} else {for(int i = x; i < x + in[pos]; i++) {vis[i][y] += val;}} } void dfs(int x) {if(x >= m + 1) {if(ok())ans ++;return ;}for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) {if(vis[i][j] || s[i][j] == 'X')continue;/// X must not placeint lim = 1;if(in[x] == 1)lim = 0;for(int t = 0; t <= lim; t++) {if(judge(x, i, j, t)) {/// can placeaddBattleship(i, j, x, t, 1);dfs(x + 1);addBattleship(i, j, x, t, -1);}}}} }int main() {cin >> n >> m;for(int i = 1; i <= n; i++)cin >> s[i] + 1;for(int i = 1; i <= m; i++)cin >> in[i];sort(in+1,in+1+m);ans = 0;dfs(1);cout << ans << endl;return 0; } /** 4 3 .... .OX. .... O..X 3 2 1**//**************************************************************Problem: 18783Language: C++Result: 正確Time:1908 msMemory:2036 kb ****************************************************************/

總結

以上是生活随笔為你收集整理的[Nowcoder] 2021年度训练联盟热身训练赛第六场 Mini Battleship | 深搜 回溯 乱搞的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。