topcoder SRM400 div2 1000point(pass system test)
来源:互联网 发布:海湾主机编程 编辑:程序博客网 时间:2024/05/16 15:05
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<sstream>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;
class LightedPanels
...{
public:
int minTouch(vector <string> board);
};
int m2(int w)
...{
int a = 1;
for(int i = 0;i<w;i++)
a *= 2;
return a;
}
bool check(int **t,int w,int i)
...{
for(int a = 0;a<w;a++)
if(t[i][a] != 1)
return false;
return true;
}
void toggle(int **t,int **tmp,int w,int h,int i,int mask)
...{
int test(0);
int a(0);
int l(0);
int b(0);
for(int x = 0;x<h;x++)
for(int j = 0;j<w;j++)
tmp[x][j] = t[x][j];
for(l = 0;l<w;l++)
...{
test = (mask>>l)& 0x00000001;
if(test == 1)
...{
for(a = i-1;a<i+2;a++)
...{
for(b = w-l-2;b<w-l+1;b++)
...{
if(!(a<0 || a==h || b<0||b==w))
tmp[a][b] = 1-tmp[a][b];
}
}
}
}
}
int ff(int **t ,int w,int h)
...{
int min(w+1);
int i(0);
int j(0);
int l(0);
int row(0);
int m2w = m2(w);
int res(0);
int mask(0);
int none(0);
int **tmp1 = new int*[h];
for(i = 0;i<h;i++)
tmp1[i] = new int[w];
int **tmp = new int*[h];
for(i = 0;i<h;i++)
tmp[i] = new int[w];
for(i = 0;i<h;i++)
for(j = 0;j<w;j++)
tmp1[i][j] = t[i][j];
for(i = 1;i<h;i++)
...{
for(l = 0;l<m2w;l++)
...{
toggle(tmp1,tmp,w,h,i,l);
if(check(tmp,w,i-1))
...{
for(j = 0;j<w;j++)
row += (l>>j)& 0x00000001;
if(row<min)
...{
min = row;
mask = l;
}
}
row = 0;
}
if(min != w+1) //find a answer
...{
res += min;
toggle(tmp1,tmp1,w,h,i,mask);
mask = 0;
min = w+1;
}
else //can't find a answer
...{
none = 1;
}
}
if(!check(tmp1,w,h-1)) //the last row
...{
none = 1;
}
for(i = 0;i<h;i++)
delete [] tmp1[i];
delete [] tmp1;
for(i = 0;i<h;i++)
delete [] tmp[i];
delete [] tmp;
if(none == 1)
return -1;
else
return res;
}
int LightedPanels::minTouch(vector <string> board)
...{
int i(0),j(0);
int w = board[0].size();
int h = board.size();
int m2w = m2(w);
int **t = new int*[h];
for(i = 0;i<h;i++)
t[i] = new int[w];
int **tmp = new int*[h];
for(i = 0;i<h;i++)
tmp[i] = new int[w];
for(i = 0;i<h;i++)
...{
for(j = 0;j<w;j++)
...{
if((board[i].c_str())[j] == '*')
t[i][j] = 1;
else
t[i][j] = 0;
}
}
for(i = 0;i<h;i++)
...{
for(j = 0;j<w;j++)
cout<<t[i][j]<<",";
cout<<endl;
}
int count(0);
int min(65);
for(i = 0;i<m2w;i++)
...{
cout<<"..........i....."<<i<<endl;
toggle(t,tmp,w,h,0,i);
count = ff(tmp,w,h);
if(count != -1)
...{
for(int l = 0;l<w;l++)
count += (i>>l)& 0x00000001;
if(count < min)
min = count;
}
}
if(min == 65)
return -1;
else
return min;
}
/**//* for(int ti = 0;ti<h;ti++)
{
for(int tj = 0;tj<w;tj++)
cout<<t[ti][tj]<<",";
cout<<endl;
}
*/
#include<stdlib.h>
#include<vector>
#include<sstream>
#include<string>
#include<math.h>
#include<algorithm>
using namespace std;
class LightedPanels
...{
public:
int minTouch(vector <string> board);
};
int m2(int w)
...{
int a = 1;
for(int i = 0;i<w;i++)
a *= 2;
return a;
}
bool check(int **t,int w,int i)
...{
for(int a = 0;a<w;a++)
if(t[i][a] != 1)
return false;
return true;
}
void toggle(int **t,int **tmp,int w,int h,int i,int mask)
...{
int test(0);
int a(0);
int l(0);
int b(0);
for(int x = 0;x<h;x++)
for(int j = 0;j<w;j++)
tmp[x][j] = t[x][j];
for(l = 0;l<w;l++)
...{
test = (mask>>l)& 0x00000001;
if(test == 1)
...{
for(a = i-1;a<i+2;a++)
...{
for(b = w-l-2;b<w-l+1;b++)
...{
if(!(a<0 || a==h || b<0||b==w))
tmp[a][b] = 1-tmp[a][b];
}
}
}
}
}
int ff(int **t ,int w,int h)
...{
int min(w+1);
int i(0);
int j(0);
int l(0);
int row(0);
int m2w = m2(w);
int res(0);
int mask(0);
int none(0);
int **tmp1 = new int*[h];
for(i = 0;i<h;i++)
tmp1[i] = new int[w];
int **tmp = new int*[h];
for(i = 0;i<h;i++)
tmp[i] = new int[w];
for(i = 0;i<h;i++)
for(j = 0;j<w;j++)
tmp1[i][j] = t[i][j];
for(i = 1;i<h;i++)
...{
for(l = 0;l<m2w;l++)
...{
toggle(tmp1,tmp,w,h,i,l);
if(check(tmp,w,i-1))
...{
for(j = 0;j<w;j++)
row += (l>>j)& 0x00000001;
if(row<min)
...{
min = row;
mask = l;
}
}
row = 0;
}
if(min != w+1) //find a answer
...{
res += min;
toggle(tmp1,tmp1,w,h,i,mask);
mask = 0;
min = w+1;
}
else //can't find a answer
...{
none = 1;
}
}
if(!check(tmp1,w,h-1)) //the last row
...{
none = 1;
}
for(i = 0;i<h;i++)
delete [] tmp1[i];
delete [] tmp1;
for(i = 0;i<h;i++)
delete [] tmp[i];
delete [] tmp;
if(none == 1)
return -1;
else
return res;
}
int LightedPanels::minTouch(vector <string> board)
...{
int i(0),j(0);
int w = board[0].size();
int h = board.size();
int m2w = m2(w);
int **t = new int*[h];
for(i = 0;i<h;i++)
t[i] = new int[w];
int **tmp = new int*[h];
for(i = 0;i<h;i++)
tmp[i] = new int[w];
for(i = 0;i<h;i++)
...{
for(j = 0;j<w;j++)
...{
if((board[i].c_str())[j] == '*')
t[i][j] = 1;
else
t[i][j] = 0;
}
}
for(i = 0;i<h;i++)
...{
for(j = 0;j<w;j++)
cout<<t[i][j]<<",";
cout<<endl;
}
int count(0);
int min(65);
for(i = 0;i<m2w;i++)
...{
cout<<"..........i....."<<i<<endl;
toggle(t,tmp,w,h,0,i);
count = ff(tmp,w,h);
if(count != -1)
...{
for(int l = 0;l<w;l++)
count += (i>>l)& 0x00000001;
if(count < min)
min = count;
}
}
if(min == 65)
return -1;
else
return min;
}
/**//* for(int ti = 0;ti<h;ti++)
{
for(int tj = 0;tj<w;tj++)
cout<<t[ti][tj]<<",";
cout<<endl;
}
*/
- topcoder SRM400 div2 1000point(pass system test)
- Topcoder SRM 514div2 500point
- TopCoder SRM 144 DIV2(200-point)
- TopCoder SRM 144 DIV2(550-point)
- TopCoder SRM625 DIV2 950-point problem
- Topcoder SRM646 DIV2 1000
- TopCoder srm683 div2 1000
- 小朋友学TopCoder(3):SRM144 DIV2 550-point
- 小朋友学TopCoder(4):SRM144 DIV2 1100-point
- Topcoder SRM536 div2 1000pt
- Topcoder SRM 635 div2 1000
- Topcoder SRM 636 div2 1000
- topcoder-srm610-div2-1000(dp)
- Topcoder SRM688 Div2 1000 ParenthesesDiv2Hard
- 小朋友学TopCoder(2):第一个TopCoder程序--SRM144 DIV2 200-point
- TopCoder SRM 634 Div2 Problem 1000 - SpecialStrings
- Topcoder SRM 640 Div2 1000(巧妙数学题)
- topcoder srm656 1000分题(div2)
- 模拟登陆os
- DB design log (10)
- Ajax 登陆控件
- spring为ApplicationContext提供有三种实现
- 宏定义中的## 连接符与# 符
- topcoder SRM400 div2 1000point(pass system test)
- 无进程DLL木马开发思路与实现
- getrlimit和setrlimit
- HDOJ 2167 Pebbles
- 发掘文件系统过滤驱动的应用
- html文件转成jsp时出现乱码
- Java 反射基础
- Struts2教程:上传任意多个文件
- 在文件过滤驱动中追踪文件状态