# NxN矩阵棋盘着色问题的符号处理，采用C语言算法编程的实现方式

Problem Description

You are given a chessboard made up of N squares by N squares with equal size. Some of the squares are colored black, and the others are colored white. Please write a program to calculate the number of rectangles which are completely made up of white squares.

Input

There are multiple test cases. Each test case begins with an integer N (1 <= N <= 100), the board size. The following N lines, each with N characters, have only two valid character values:

# - (sharp) representing a black square;

. - (point) representing a white square.

Process to the end of file.

Output

For each test case in the input, your program must output the number of white rectangles, as shown in the sample output.

Sample Input

2

.#

..

4

..#.

##.#

.#..

.#.#

Sample Output

5

15

- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答

提交

#### 相关推荐

- 2年前回答 3 已采纳 ![图片说明](https://img-ask.csdn.net/upload/201901/04/1546611499_210346.png) 我的答案： ``` #include #define N 10 void Transpose(int (*a)[N], int n); void Swap(int *x, int *y); void InputMatrix(int (*a)[N], int n); void PrintMatrix(int (*a)[N], int n); void InputMatrix(int (*a)[N], int n) { int i,j; for(i = 0;i < n ;i++) { for(j = 0;j < n; j++) { scanf("%d",&a[i*n + j]); } } } void Transpose(int (*a)[N], int n) { int i,j; for(i = 0;i < n ;i++) { for(j = 0;j < n; j++) { Swap(&a[i*n + j], &a[j*n + i]); } } } void Swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } PrintMatrix(int (*a)[N], int n) { int i,j; for(i = 0;i < n ;i++) { for(j = 0;j < n ; j++) { printf("%d\t", *a[i*n + j]); } printf("\n"); } } int main() { int n,s[N][N]; printf("Input n:"); scanf("%d",&n); printf("Input %d*%d matrix:\n",n,n); InputMatrix( s, n); Transpose( s, n); printf("The transposed matrix is:\n"); PrintMatrix( s, n); return 0; } ``` 请问哪里错了？该怎么改？
- 回答 1 已采纳 ![图片说明](https://img-ask.csdn.net/upload/201912/02/1575263051_202456.png) ``` #include #define N 6 int main() { int a[N][N],i,j,k=1; for(i=0;i<N;i++;) { for(j=0;i<N;j++) printf("%4d",a[i][j]); printf("\n"); } return 0; } ```
- 5年前回答 1 已采纳 Description Go is played on a square board with an odd number of vertical and horizontal lines. The usual board sizes are 9x9, 13x13 and 19x19. But we'll assume the size in nxn for 3 ≤ n ≤ 19. Black and White alternately play stones on the intersection between two lines. Black starts. At any time one player may pass – not play a stone – but if both players pass the game ends. We'll denote playing a stone by P(x,y) where P is either B (for Black) or W (for White) and (1−n)/2 ≤ x,y ≤ (n−1)/2 gives the grid position of the stone to be played. The centre intersection of the board has coordinates (0,0). The rules of Go are reasonably straightforward, but the nuances of strategy make it an extremely challenging game. You are to use the following rules. Black plays first. Black and White alternate; at each turn a player may place a stone or may pass. The game ends when Black and White pass consecutively. A stone may be played only on an unoccupied intersection. If one player P places a stone so that his or her stones (along with the edge of the board) completely surround a connected area occupied by stones belonging to the other player, Q, Q's stones are said to be captured and removed from the board. More precisely, two intersections are connected if they are horizontally or vertically (but not diagonally) adjacent. Stones in an area are completely surrounded if no stone is connected with a vacant intersection. If P places a stone that causes Q's stones to be captured, P's stone is not captured. A connected area surrounded by P's stones which contains none of Q's stones is said to be owned by P. The score for player P is the number of vacant intersections owned by P in the final board configuration plus the number of Q's stones captured by P at any time during the game. Input The input consists of several test cases. Each test case begins with a line containing n – the size of the board – and m – the number of stones placed in the game. m lines follow, each giving a placement in the format above. Note that m counts only stone placements – passes may result in two consecutive placements by the same player. You may assume that each move is legal. A line containing 0 0 follows the last test case. Output For each test case, output a line with two numbers: Black's score followed by White's score. Sample Input 7 6 B(-2,-2) W(2,2) B(-2,-3) W(2,3) B(-3,-2) W(3,2) 7 6 B(-2,-3) W(-3,-3) B(-2,-2) W(3,2) B(-3,-2) W(2,3) 0 0 Sample Output 1 1 2 1
- 4年前回答 1 已采纳 Problem Description A typical strategy in the game Starcraft is to mass up large amounts of low-tier units such as Zerglings, then throw them at your opponent and laugh maniacally as they overwhelm any opposition. However, when both players opt for the same strategy, the result can often become...quick, brutal and messy. Sadly, the game only allows for up to 400 Zerglings per player. In this problem, however, you will simulate games that could have more than 400 Zerglings. The map on which Zerg rushes occur is represented by a NxN grid. Each Zergling occupies a grid square and no two Zerglings ever occupy the same grid square. Each Zergling has a certain number of hit points which starts off as 35. Its attack value is 5 plus the attack upgrade of the player that controls it. When one Zergling attacks another, the damage incurred equals to the attack value of the attacking Zergling minus the armour upgrade of the player that owns the Zergling being attacked. The number of hit points of the Zergling that is attacked is reduced by the amount of the damage. Due to the inability of both players to manage their huge horde of Zerglings, the Zerglings make their decisions each turn using the following algorithm (Zerglings are not the brightest creatures, as you will see): * If there is an opponent Zergling in one of the 8 horizontally, vertically, or diagonally adjacent grid squares, the Zergling will attack it. A Zergling attacks at most one opponent each turn; see below for the tie-breaking rules. * Otherwise, if the other player has at least one Zergling remaining on the map, the Zergling will move to the horizontally, vertically, or diagonally adjacent square that is closest to the opponent's closest Zergling in terms of Manhattan distance. When more than one adjacent square is closest, the tie-breaking rules below are used. The Manhattan distance between two points is the sum of the differences in the x and y coordinates of the points. When the above rules could cause the Zergling to attack in more than one direction, or to move in more than one direction, the following tie-breaking rule is used. The Zergling will prefer the first direction starting with north going clockwise. That is, the directions in order of preference are north, northeast, east, southeast, etc. Once all Zerglings have made their decisions, all the attacks are conducted simultaneously and all the Zerglings with 0 or fewer hit points are marked as dead and removed from the map. Then all the movements of the Zerglings that didn't attack are conducted simultaneously. If the square to which a Zergling is moving is occupied by another Zergling that is not moving away in this turn, then the Zergling does not move. If two or more Zerglings try to move to the same grid square, then the Zergling in the northernmost row has the right of way and the other Zergling remains stationary. If there are multiple Zerglings in the northernmost row trying to move to the same grid square, then of these, the westernmost Zergling moves and the others remain stationary. Zerglings also have a remarkable regeneration rate. After each turn, all the Zerglings that are still alive and have less than 35 hitpoints will regain one hit point. Input The input file contains a number of test cases, ended by a case with N=0. Each case begins with N between 2 and 150, followed by 2 pairs of 2 integers between 0 and 3, the attack and armour upgrades of the first player, followed by the attack and armour upgrades of the second player. This is followed by the initial game map, where '.' denotes an empty square, '1' a Zergling belonging to the first player and '2' a Zergling belonging to the second player. On the map, north is up (i.e. towards the first row) and west is left (i.e. towards the first column). Finally, the test case provides the number t of turns for which the Zerg rush is to be simulated, which is an integer between 0 and 400, inclusive. Output For each input case, output the map after t turns in the same format as above. Print one empty line between the output for consecutive test cases. Sample Input 2 0 0 0 0 1. .. 0 2 0 0 0 0 1. .2 100 0 Sample Output 1. .. .. ..
- 4年前回答 1 已采纳 Recently, the organizers of the International Conference on Functional Programming had a programming contest for people to show that their language was best. The contest main web page is at http://www.ai.mit.edu/extra/icfp-contest/. Each entrant had to write an implementation of the game "pouse", for which the description went as follows: Description of the game The name of the game is "pousse" (which is French for "push"). It is a 2 person game, played on an N by N board (the original game was 4x4 but NxN is a simple generalisation to adjust the difficulty of the game, and its implementation). Initially the board is empty, and the players take turns inserting one marker of their color (X or O) on the board. The color X always goes first. The columns and rows are numbered from 1 to N, starting from the top left, as in: 1 2 3 4 +-+-+-+-+ 1 | | | | | +-+-+-+-+ 2 | | | | | +-+-+-+-+ 3 | | | | | +-+-+-+-+ 4 | | | | | +-+-+-+-+ A marker can only be inserted on the board by sliding it onto a particular row from the left or from the right, or onto a particular column from the top or from the bottom. So there are 4*N possible "moves" (ways to insert a marker). They will be named "Li", "Ri", "Ti", "Bi" respectively, where "i" is the number of the row or column where the insertion takes place. When a marker is inserted, there may be a marker on the square where the insertion takes place. In this case, all markers on the insertion row or column from the insertion square upto the first empty square are moved one square further to make room for the inserted marker. Note that the last marker of the row or column will be pushed off the board (and must be removed from play) if there are no empty squares on the insertion row or column. A row or a column is a "straight" of a given color, if it contains N markers of the given color. The game ends when an insertion creates a configuration with more straights of one color than straights of the other color; the player whose color is dominant (in number of straights) WINS. Input The standard input of the program will contain a number N <= 100 on the first line and this will be followed by a sequence of moves (in the notation previously described) with one move per line. There are no intervening spaces or empty lines. The program can assume that all moves in the sequence are valid. Process to the end of file. Output The organizers then want to play one submitted program against the others to see which is best. So they need to know when one program wins. Your job is to write a program to determine when a game has been won. The input to your program is the same as described above: an initial number followed by a sequence of moves. As soon as a move produces a winning board position, your program should print out whether "X WINS" or "O WINS", and exit. If a line containing QUIT is read before a winner is declared, your program should print out "TIE GAME" and exit. As a failsafe, the last line of every input will be a QUIT line. Sample Input 4 L2 T2 L2 B2 R2 QUIT 4 L2 T2 L2 B2 R2 T1 L2 QUIT Sample Output TIE GAME X WINS
- 12年前回答 2 已采纳 int num[3][4]; for(i=0;i <3;i++) for(t=0;t <4;t++) num[i][t] = (i*4)+t+1; 上面代码初始化1到12的二维矩阵，由于数据很规律，很容易得到(i*4)+t+1赋值语句， 一般二维数组的初始化也都是根据实际情况来判断， 比如 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 该矩阵中的数字很规则，在实际解决该问题时，只需要把数值的规律描述出来即可。 实现思路：声明一个变量n，代表矩阵的阶，声明和初始化一个nXn的数组，根据数据的规律，则对应的数值为(行号 + 列号 + 1)，当数值比n大时，取和n的余数 根据判断都可以得到赋值语句，可是假如我们的数据很复杂，但从判断很难得到这个初始化赋值语句，那该怎么办，有没有规律可循？ 换句话说，对应一个矩阵怎么样用数学归纳法证明矩阵的i行j列的数据等于(i*4)+t+1,或者是(行号 + 列号 + 1)或者是其他的什么东西？
- 4年前回答 1 已采纳 Problem Description A chessboard is a NxN binary matrix with rows and columns numbered from 1 to N. Each position of the matrix is black (1) if the sum of the row number and the column number is even; otherwise it is white (0). The following pictures show how a chessboard looks like for N=1, 2 and 3. Given a NxN binary matrix, find the size of the largest chessboard completely located inside the matrix, as well as the number of chessboards having the largest size (these chessboards may overlap). Input The first line of input contains an integer number T, representing the number of test cases to follow. Each test case contains on the first line an integer number N (1<=N<=2000), representing the number of rows and columns of the given matrix. The next N lines describe the matrix: each line contains N characters, which may be either ‘1’ (denoting a black square) or ‘0’ (denoting a white square); at the end of each line there will be a new line character. The matrix will contain at least one ‘1’ character. Output For each of the T test cases, in the order given in the input, print one line containing the number of rows and colums of the largest chessboard, followed by a blank and then the number of chessboards having the largest size. Sample Input 1 5 00101 11010 00101 01010 11101 Sample Output 3 3
- 4年前回答 1 已采纳 Problem Description FSF has programmed a game. In this game, players need to divide a rectangle into several same squares. The length and width of rectangles are integer, and of course the side length of squares are integer. After division, players can get some coins. If players successfully divide a AxB rectangle(length: A, width: B) into KxK squares(side length: K), they can get A*B/ gcd(A/K,B/K) gold coins. In a level, you can’t get coins twice with same method. (For example, You can get 6 coins from 2x2(A=2,B=2) rectangle. When K=1, A*B/gcd(A/K,B/K)=2; When K=2, A*B/gcd(A/K,B/K)=4; 2+4=6; ) There are N*(N+1)/2 levels in this game, and every level is an unique rectangle. (1x1 , 2x1, 2x2, 3x1, ..., Nx(N-1), NxN) FSF has played this game for a long time, and he finally gets all the coins in the game. Unfortunately ,he uses an UNSIGNED 32-BIT INTEGER variable to count the number of coins. This variable may overflow. We want to know what the variable will be. (In other words, the number of coins mod 2^32) Input There are multiply test cases. The first line contains an integer T(T<=500000), the number of test cases Each of the next T lines contain an integer N(N<=500000). Output Output a single line for each test case. For each test case, you should output "Case #C: ". first, where C indicates the case number and counts from 1. Then output the answer, the value of that UNSIGNED 32-BIT INTEGER variable. Sample Input 3 1 3 100 Sample Output Case #1: 1 Case #2: 30 Case #3: 15662489
- 回答 3 已采纳 Erlang中的变量大部分情况下是"不可变"的(进程数据区域等特殊的除外), 据称是和并行相关, 不过这里我比较困惑, 既然Erlang进程设置了消息系统, 理论上讲消息都是一个一个处理的, 即:同一个进程中的消息处理不存在并行情况, 因此无需要"边量不变". 如果是在多进程情况下, 由于进程的数据之间是切分开的, 看起来也不太需要"变量不变"这个特性, why??? 可否举个例子说明在哪种情况下需要"变量不变"这个特性??? 备注: 本人初学Erlang, 理解错误的地方希望前辈们指点. Thanks. [b]问题补充：[/b] 根据night_stalker的回答, 看起来immutable是和"并行"没有太大关系的了... 还有别的方面的特性决定需要使用immutable的么??? [b]问题补充：[/b] 感谢night_stalker, 又去查了下资料, 应该是和进程调度有关, 详细见这里 : http://hi.baidu.com/timeless/blog/item/3f3dafc3cb84835db319a8ae.html 感觉上是由于immutable的特性决定进程可以比较轻松的进行调度而不消耗太多资源, 想比传统的进程/线程方式就有了很多好出. 另, to night_stalker, Erlang里应该不直接支持global-var的把...每个进程都有自己独立的数据区域, 相互之间应该是不share的... 再次感谢, 对这个问题有了更深入的认识:)~
- 回答 1 已采纳 在看《erlang程序设计》中讲到5.3.2中提到的比特语法表达式里有这样的写法，但是翻来翻去都没找到关于#语法的解释 请指教！多谢
- 南 墙的博客 其中一个nxn的矩阵除m的余数得到的仍是一个nxn的矩阵，这个矩阵的每一个元素是原矩阵对应位置上的数除m的余数。 要计算这个问题，可以将A连乘b次，每次都对m求余，但这种方法特别慢，当b较大时无法使用。下面给...
- dream_uping的博客 这个我做了改进，可以实现NXN的矩阵。求出对角线之和！ 只需要修改定义的define z的值就好！ 接下来，进入正题！ 题目描述： 求一个3×3矩阵对角线元素之和。 输入 矩阵 输出 主对角线 副对角线 元素和 ...
- 1年前elsieyin的博客 #include<stdio.h> #include<stdlib.h> #define M 3 int main(void) { int i,j,k,matrix1[M][M],matrix2[M][M],row1=M ,col1=M ,row2=M,col2=M,matrix[M]... /*为需要相乘的两个矩阵赋值：*/ prin...
- 努力奋斗的小青年的博客 #include <stdio.h> #define N 5 int main() { int a[N][N],i,j,k,m; if(N%2==0) m=N/2; else m=N/2+1; for(i=0;i<m;i++) { for(j=i;j<N-i;j++) a[i][j]=a[N-i-1][j]=i+1;...N-.
- 1年前大阳的男人的博客 1、简介 给出一个矩阵，顺时针打印矩阵的数据 比如 int buf [4][4] = { {1,2,3,4}, ...打印出来的样式应该为1、2、3、4、8、12、16、15、14、13、9、5、6、7、11、10这样的顺时针...1、先实现矩阵的逆时针翻转 ...
- it_xiangqiang的博客 C语言矩阵N*N旋转的算法C语言矩阵N*N旋转的算法完整源码(定义，实现，main函数测试） C语言矩阵N*N旋转的算法完整源码(定义，实现，main函数测试） #include<iostream> void helper_transpose(int **matrix, ...
- WhiteShirtI的博客 算法流程 1、当 arr为空时，直接返回空列表 [] 即可。 2、矩阵 左、右、上、下 四个边界 l , r , t , b ，用于打印的结果列表 val 。 3、“从左向右、从上向下、从右向左、从下向上” 四个方向循环，每个方向打印中...
- 红黄蓝幼儿园的博客 通过行指针实现 函数接口定义： void tian(int (*p)[n]) ; 其中 p 是用户传入的参数。 函数通过行指针 p 填充方阵，其规则是一个n×n方阵，副对角线填1，右下三角填2，左上三角填3。 裁判测试程序样例： #include &...
- z-j-w的博客 c语言解决-分治法在数值问题中的应用—矩阵相乘问题 实验题目 设M1和M2是两个n×n的矩阵，设计算法计算M1×M2 的乘积。 2．实验目的 1）提高应用蛮力法设计算法的技能； 2）深刻理解并掌握分治法的设计思想； 3...
- 3年前额di个神的博客 本文转载自http://www.cnblogs.com/scarecrow-blog/p/3712580.html【问题描述】给定n个矩阵｛A1,A2,…,An｝，其中Ai与Ai+1是可...例如，给定三个连乘矩阵{A1，A2，A3}的维数分别是10*100，100*5和5*50，采用（A1A2）...