首页 > ACM题库 > HDU-杭电 > HDU 1838 Chessboard[解题报告] C++
2013
12-23

HDU 1838 Chessboard[解题报告] C++

Chessboard

问题描述 :

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).

输入:

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.

输出:

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.

样例输入:

1
5
00101
11010
00101
01010
11101

样例输出:

3 3

很简单的一题找规律题。

我的解题代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>

using namespace std;


int main()
{
	int N;
	while(cin >> N && N!=0)
	{
		int level = floor(sqrt(double(N-1)))+1;
		int middle = (level-1)*(level-1)+level;
//		cout << "level: " << level << " middle: " << middle << endl;
		if(level%2) 
		{
			if(N<middle) cout << level << ' ' << level+N-middle << endl;
			else cout << level-N+middle << ' ' << level << endl;
		}
		else 
		{
			if(N<middle) cout << level+N-middle << ' ' << level << endl;
			else cout << level << ' ' << level-N+middle << endl;
		}
	}
	return 0;
}

解题报告转自:http://blog.csdn.net/rising_fallmoon/article/details/9731713


  1. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }