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; }
相关文章推荐
- 解决sharepoint 2010浏览器在线浏览Word出错
- FWNX-Evaluate Reverse Polish Notation -c++ VERSION-solution accepted
- DP22 堆盒子问题 Box Stacking Problem @geeksforgeeks
- 理解NSTextContainer
- bootstrap 和传统的html 关于button的属性问题
- GIT在Linux上的安装和使用简介
- 未来三年半,要和姑娘享受亲密吗?
- 给贺老师的一封邮件(问题是如何(HOW)去运用实践,去编程!!,去运用这些计算机科学与技术理论课程)
- stl string的substr和find_first_of函数的一点心得
- 将SVG转换为位图的方法(保持浏览器显示的样式)
- SRM 390(1-250pt)
- Apache Archiva: Maven服务端管理软件
- sql语句,查询时间段的数据
- CSS & HTML5 video switch
- SRM 391(1-250pt)
- linux shell 脚本攻略学习13--file命令详解,diff命令详解
- django之一
- DP21 LIS的变种问题-建桥问题 Variations of LIS-Building Bridge @geeksforgeeks
- Proxy实现java动态代理
- Order Authentic Cheap Jordan Gamma Blue 12s only $89