您的位置:首页 > 其它

MINESWEEPER题解

2013-12-28 04:37 274 查看
这题也比较简单,只要先把参数从文件读出来,然后做一组判断即可,这题没有看第二遍的价值。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string convertStr(string str, int m, int n) {
int num;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
num = 0;
if(str[i*n+j] == '*') continue;
if(i>=1) {
if(str[(i-1)*n+j] == '*') ++num;
if(j>=1)
if(str[(i-1)*n+j-1] == '*') ++num;
if(j<n-1)
if(str[(i-1)*n+j+1] == '*') ++num;
}
if(i<m-1) {
if(str[(i+1)*n+j] == '*') ++num;
if(j<n-1)
if(str[(i+1)*n+j+1] == '*') ++num;
if(j>=1)
if(str[(i+1)*n+j-1] == '*') ++num;
}
if(j>=1) {
if(str[i*n+j-1] == '*') ++num;
}
if(j<n-1){
if(str[i*n+j+1] == '*') ++num;
}
str[i*n+j] = num+'0';
}
}
return str;
}

int main (int argc, char* argv[])
{
ifstream file;
string lineBuffer;

file.open(argv[1]);
while (!file.eof()) {
getline(file, lineBuffer);
if (lineBuffer.length() == 0)
continue; //ignore all empty lines
else {
string str1, str2;
int M, N;
int start = lineBuffer.find_first_of(";");
str1 = lineBuffer.substr(0, start);
str2 = lineBuffer.substr(start+1);
sscanf(str1.c_str(), "%d,%d", &M, &N);
cout << convertStr(str2, M, N) << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: