Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我枚举写的 算dfs吧 也只要204ms 为什么bfs dfs会这么大#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <vector> #include <map> #include <math.h> using namespace std; typedef long long ll; #define clr(s,x) memset(s,x,sizeof(s)) #define Max(a,b) a > b ? a : b #define Min(a,b) a > b ? b : a char s[4][4]; int vis[4][4]; bool press[4][4]; void init() { for(int i = 0 ; i < 4; i ++) { for(int j = 0; j < 4; j++) scanf("%c",&s[i][j]); getchar(); } clr(vis,0); clr(press,0); } void change(int x,int y) { for(int i = 0 ; i < 4; i++) vis[x][i] ++; for(int i = 0; i < 4; i++) vis[i][y]++; vis[x][y]--; press[x][y] = press[x][y] ? false: true; } bool check() { for(int i= 0 ; i < 4 ; i++) for(int j = 0; j < 4; j++) if( (s[i][j] == '+' && vis[i][j]%2 )|| (s[i][j] == '-' && !(vis[i][j]%2))) continue; else return false; /*for(int i = 0 ; i < 4; i ++) { for(int j = 0; j < 4; j++) cout<<vis[i][j]<<" "; cout<<endl; }*/ return true; } void output() { int ans = 0; for(int i = 0 ; i < 4; i++) for(int j =0 ; j < 4; j++) if(press[i][j]) ans++; printf("%d\n",ans); for(int i = 0 ; i < 4; i++) for(int j =0 ; j < 4; j++) if(press[i][j]) printf("%d %d\n", i+1,j+1); } bool solve(int x,int y) { if(x == 3 && y== 3) { if(check()) return true; change(3,3); if(check()) return true; change(3,3); return false; } int nx,ny; if(y == 3) ny = 0,nx = x + 1; else nx = x, ny = y+1; if(solve(nx,ny)) return true; change(x,y); if(solve(nx,ny)) return true; change(x,y); return false; } int main() { init(); if(solve(0,0)) output(); else printf("wa\n"); /*for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) cout<<s[i][j];*/ return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator