Poj 2870 Light Up brute force search + pruning

Source: Internet
Author: User

After reading the questions for a long time, I thought there was a profound method. I didn't expect the last Brute Force pruning to be completed. Sorry, but it was still TLE many times ......
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Int mp [10] [10];
Int n, m, lim;
Bool den [10] [10];
Bool valid (int x, int y)
{
Return x> = 1 & x <= n & y> = 1 & y <= m;
}
Bool check ()
{
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
If (mp [I] [j] =-2) return false;
If (mp [I] [j]> = 1 & mp [I] [j] <= 4) return false;
}
}
Return true;
}
Int dir [4] [2] = {,-, 0,-1 };
Bool canput (int x, int y)
{
For (int I = 0; I <4; I ++)
{
Int tx = x + dir [I] [0];
Int ty = y + dir [I] [1];
If (valid (tx, ty ))
{
If (mp [tx] [ty] = 0) return false;
}
}
Return true;
}
Void go (int x, int y)
{
If (mp [x] [y] =-2)
{
Mp [x] [y] = 5;
Return;
}
If (mp [x] [y]> = 5) mp [x] [y] ++;
}
Void gao (int x, int y)
{
For (int I = 0; I <4; I ++)
{
Int tx = x + dir [I] [0];
Int ty = y + dir [I] [1];
If (valid (tx, ty ))
{
If (mp [tx] [ty]> = 1 & mp [tx] [ty] <= 4) mp [tx] [ty] --;
}
}
For (int I = y + 1; I <= m; I ++)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = Y-1; I> = 1; I --)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = X-1; I> = 1; I --)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
For (int I = x + 1; x <= n; I ++)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
}
Void Go (int x, int y)
{
If (mp [x] [y] = 5) mp [x] [y] =-2;
If (mp [x] [y]> 5) mp [x] [y] --;
}
Void clear (int x, int y)
{
For (int I = 0; I <4; I ++)
{
Int tx = x + dir [I] [0];
Int ty = y + dir [I] [1];
If (valid (tx, ty ))
{
If (mp [tx] [ty]> = 0 & mp [tx] [ty] <= 3) mp [tx] [ty] ++;
}
}
For (int I = y + 1; I <= m; I ++)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = Y-1; I> = 1; I --)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = X-1; I> = 1; I --)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
For (int I = x + 1; x <= n; I ++)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
}
Int ans;
Void dfs (int dep, int x, int y)
{
If (dep> ans) return;
If (x> n)
{
If (check ())
{
If (dep <ans) ans = dep;
}
Return;
}
If (x> 1 & mp [x-1] [y] =-2 & mp [x] [y]> =-1 & mp [x] [y] <= 4) return;
If (y <m) dfs (dep, x, y + 1 );
Else dfs (dep, x + 1, 1 );
If (mp [x] [y] =-2)
{
If (canput (x, y ))
{
Den [x] [y] = true;
Mp [x] [y] = 5;
Gao (x, y );
If (y <m) dfs (dep + 1, x, y + 1 );
Else dfs (dep + 1, x + 1, 1 );
Den [x] [y] = false;
Mp [x] [y] =-2;
Clear (x, y );
}
}
}
Int main ()
{
Int B, r, c, k;
While (scanf ("% d", & n, & m), n | m)
{
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Mp [I] [j] =-2;
}
}
Scanf ("% d", & B );
For (int I = 0; I <B; I ++)
{
Scanf ("% d", & r, & c, & k );
Mp [r] [c] = k;
}
Ans = 1000;
Dfs (0, 1 );
If (ans <1000) printf ("% d \ n", ans );
Else puts ("No solution ");
}
Return 0;
}

# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
Int mp [10] [10];
Int n, m, lim;
Bool den [10] [10];
Bool valid (int x, int y)
{
Return x> = 1 & x <= n & y> = 1 & y <= m;
}
Bool check ()
{
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
If (mp [I] [j] =-2) return false;
If (mp [I] [j]> = 1 & mp [I] [j] <= 4) return false;
}
}
Return true;
}
Int dir [4] [2] = {,-, 0,-1 };
Bool canput (int x, int y)
{
For (int I = 0; I <4; I ++)
{
Int tx = x + dir [I] [0];
Int ty = y + dir [I] [1];
If (valid (tx, ty ))
{
If (mp [tx] [ty] = 0) return false;
}
}
Return true;
}
Void go (int x, int y)
{
If (mp [x] [y] =-2)
{
Mp [x] [y] = 5;
Return;
}
If (mp [x] [y]> = 5) mp [x] [y] ++;
}
Void gao (int x, int y)
{
For (int I = 0; I <4; I ++)
{
Int tx = x + dir [I] [0];
Int ty = y + dir [I] [1];
If (valid (tx, ty ))
{
If (mp [tx] [ty]> = 1 & mp [tx] [ty] <= 4) mp [tx] [ty] --;
}
}
For (int I = y + 1; I <= m; I ++)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = Y-1; I> = 1; I --)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = X-1; I> = 1; I --)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
For (int I = x + 1; x <= n; I ++)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
}
Void Go (int x, int y)
{
If (mp [x] [y] = 5) mp [x] [y] =-2;
If (mp [x] [y]> 5) mp [x] [y] --;
}
Void clear (int x, int y)
{
For (int I = 0; I <4; I ++)
{
Int tx = x + dir [I] [0];
Int ty = y + dir [I] [1];
If (valid (tx, ty ))
{
If (mp [tx] [ty]> = 0 & mp [tx] [ty] <= 3) mp [tx] [ty] ++;
}
}
For (int I = y + 1; I <= m; I ++)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = Y-1; I> = 1; I --)
{
Go (x, I );
If (mp [x] [I] >=- 1 & mp [x] [I] <= 4) break;
}
For (int I = X-1; I> = 1; I --)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
For (int I = x + 1; x <= n; I ++)
{
Go (I, y );
If (mp [I] [y] >=- 1 & mp [I] [y] <= 4) break;
}
}
Int ans;
Void dfs (int dep, int x, int y)
{
If (dep> ans) return;
If (x> n)
{
If (check ())
{
If (dep <ans) ans = dep;
}
Return;
}
If (x> 1 & mp [x-1] [y] =-2 & mp [x] [y]> =-1 & mp [x] [y] <= 4) return;
If (y <m) dfs (dep, x, y + 1 );
Else dfs (dep, x + 1, 1 );
If (mp [x] [y] =-2)
{
If (canput (x, y ))
{
Den [x] [y] = true;
Mp [x] [y] = 5;
Gao (x, y );
If (y <m) dfs (dep + 1, x, y + 1 );
Else dfs (dep + 1, x + 1, 1 );
Den [x] [y] = false;
Mp [x] [y] =-2;
Clear (x, y );
}
}
}
Int main ()
{
Int B, r, c, k;
While (scanf ("% d", & n, & m), n | m)
{
For (int I = 1; I <= n; I ++)
{
For (int j = 1; j <= m; j ++)
{
Mp [I] [j] =-2;
}
}
Scanf ("% d", & B );
For (int I = 0; I <B; I ++)
{
Scanf ("% d", & r, & c, & k );
Mp [r] [c] = k;
}
Ans = 1000;
Dfs (0, 1 );
If (ans <1000) printf ("% d \ n", ans );
Else puts ("No solution ");
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.