36. Valid Sudoku
2016-09-12 20:03
274 查看
#include<iostream> #include<vector> #include<map> using namespace std; class Solution { public: bool isValidSudoku(vector<vector<char> >& board) { int i,j; for(i=0;i<9;i++) { map<char,int>temp1; map<char,int>temp2; for(j=0;j<9;j++) { if(board[i][j]!='.'&&temp1.find(board[i][j])!=temp1.end()) return false; else ++temp1[board[i][j]]; if(board[j][i]!='.'&&temp2.find(board[j][i])!=temp2.end()) return false; else ++temp2[board[j][i]]; } } int m,n; for(i=0;i<3;i++) { for(j=0;j<3;j++) { map<char,int>temp; for(m=3*i;m<3*i+3;m++) { for(n=3*j;n<3*j+3;n++) { if(temp.find(board[m] )!=temp.end()&&board[m] !='.') return false; else ++temp[board[m] ]; } } } } return true; } }; int main() { vector<vector<char> >board; char x; int i,j; vector<char>v; for(i=0;i<9;i++) board.push_back(v); for(i=0;i<9;i++) for(j=0;j<9;j++) { cin>>x; board[i].push_back(x); } Solution solve; cout<<solve.isValidSudoku(board)<<endl; return 0; }
相关文章推荐
- 关于栈、堆、静态变量区的访问效率
- 关于指针定义的一些问题
- 关于C++ 11的新特性
- 进制转换并打印
- 函数指针
- Linux下C语言的内存对齐
- Linux下C语言的内存对齐
- C/C++的字符串流
- c/c++的笔记(看到想记录的就添加)
- 使用WH_MOUSE_LL钩子来判断按键是否是mouse_event模拟的
- MinGW环境变量设置、CodeBlock添加额外的库
- NodeJS C/C++扩展之Hello World~
- 走进BLAS/LAPACK(1)
- 走进BLAS/LAPACK(2)--blas
- C语言宏详解
- websocket C/C++服务器应用
- 内存分配学习(一)-- 实现一个malloc
- 逗号运算符与加加减减
- C/C++ struct 成员对齐
- C/C++数据类型总结