ChessFloor
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
TC
Description
Samantha is renovating a square room. The floor of the room is an N times N grid of unit square tiles. Each tile has some color. You are given the current colors of all tiles in a vector <string> floor with N elements, each containing N characters. Each character represents one tile. Identical characters represent tiles of the same color.
Samantha wants to be able to play chess or checkers on the floor. Hence, she wants to change the entire floor into a checkerboard pattern. A checkerboard pattern has two properties:
there are exactly two distinct colors of tiles
no two tiles of the same color share a common side
For example, this is a checkerboard pattern:
afa
faf
afa
This is not a checkerboard pattern because there are more than two distinct colors:
aba
bcb
aba
This is not a checkerboard pattern because there are two tiles that share a side and have the same color:
aaa
bab
aba
Samantha wants to change her floor into a checkerboard pattern by changing the colors of as few tiles as possible. Compute and return the number of tiles she needs to change.
Input
-
N will be between 2 and 20, inclusive.
-
floor will contain exactly N elements.
-
Each element of floor will consist of exactly N characters.
-
Each character in floor will be a lowercase English letter ('a'-'z').
Output
Class:
ChessFloor
Method:
minimumChanges
Parameters:
vector <string>
Returns:
int
Method signature:
int minimumChanges(vector <string> floor)
(be sure your method is public)
Sample Input
{"wbwbwbwb",
"bwbwbwbw",
"wbwbwbwb",
"bwbwbwbw",
"wbwbwbwb",
"bwbwbwbw",
"wbwbwbwb",
"bwbwbwbw"}
Sample Output
0
HINT
题意
给你一个n*n的棋盘,让你修改最少的字母,让这个棋盘形成相间的模样
题解:
暴力!不要想多了,就直接暴力!
代码
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; class ChessFloor{ public: int minimumChanges(vector <string> floor){ int ans=999999999; for(int i=0;i<26;i++) { for(int j=0;j<26;j++) { if(i==j) continue; int ans1=0; for(int ii=0;ii<floor.size();ii++) { for(int jj=0;jj<floor.size();jj++) { if((ii+jj)%2==0) { if(floor[ii][jj]!=char(i+'a')) ans1++; } else { if(floor[ii][jj]!=char(j+'a')) ans1++; } } } ans=min(ans,ans1); } } return ans; } };