#
模拟..wa了一次..因为用文件做的..忘了去掉文件了..
而且一开始看错了题...
导致最后改的时候代码乱七八糟的..orz..
#include <iostream>
using namespace std;
char pie_1[6]={'K','Q','R','B','N','P'};
char pie_2[15]={'k','q','r','b','n','p'};
bool hash[18][34];
char ch[18][34];
int main()
{
for (int i=1;i<=17;i++)
for (int j=1;j<=33;j++)
cin>>ch[i][j];
cout<<"White: ";
//find White piece
bool first=false;
memset(hash,0,sizeof(hash));
for (int k=0;k<6;)
{
bool is=false;
for (int i=17;i>=1&&(!is);i--)
{
for (int j=1;j<=33&&(!is);j++)
{
if (hash[i][j]) continue;
if (ch[i][j]==pie_1[k])
{
hash[i][j]=true;
is=true;
if (!first)
{cout<<pie_1[k]<<(char)(j/4+1+96)<<8-i/2+1;first=true;}
else
{
if (k>4) cout<<","<<(char)(j/4+1+96)<<8-i/2+1;
else
cout<<","<<pie_1[k]<<(char)(j/4+1+96)<<8-i/2+1;
}
}
}
}
if (!is) k++;
}
cout<<endl;
first=false;
cout<<"Black: ";
memset(hash,0,sizeof(hash));
for (int k=0;k<6;)
{
bool is=false;
for (int i=1;i<=17&&(!is);i++)
{
for (int j=1;j<=33&&(!is);j++)
{
if (hash[i][j]) continue;
if (ch[i][j]==pie_2[k])
{
hash[i][j]=true;
is=true;
if (k==0)
{ cout<<pie_1[k]<<(char)(j/4+1+96)<<8-i/2+1;first=true;}
else
{
if (k>4) cout<<","<<(char)(j/4+1+96)<<8-i/2+1;
else
cout<<","<<pie_1[k]<<(char)(j/4+1+96)<<8-i/2+1;
}
}
}
}
if (!is) k++;
}
cout<<endl;
return 0;
}
我绝望.我检讨..模拟..
一直pe..提交若干次后才发现是格式里多了个空格.
以后这个pe我是记住了..
#include <iostream>
using namespace std;
int n,m,x,y,u;
char ch[11][11];
bool hash[11][11];
int st[11][11];
void dfs(int x,int y,int step)
{
st[x][y]=step;
hash[x][y]=true;
switch(ch[x][y])
{
case 'N':x--;break;
case 'E':y++;break;
case 'W':y--;break;
case 'S':x++;break;
default:break;
}
if (x<1||y<1||x>n||y>m)
{
cout<<step<<" step(s) to exit"<<endl;
return;
}
if (hash[x][y])
{
cout<<st[x][y]-1<<" step(s) before a loop of "<<step-st[x][y]+1<<" step(s)"<<endl;
return;
}
dfs(x,y,step+1);
}
int main()
{
while(1)
{
cin>>n>>m>>u;
if (n==0&&m==0&&u==0) break;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
{
cin>>ch[i][j];
hash[i][j]=false;
}
// memset(hash,0,sizeof(hash));
dfs(1,u,1);
}
return 0;
}
贪心
求出每个岛屿被雷达覆盖,雷达位置的最左点和最右点
按左值排序
从左向右依次扫描
把雷达的位置放在最右点,如果某岛屿不在范围,就添加雷达
#include <iostream>
#include <math.h>
using namespace std;
const int MAXN=1001;
int n,d;
int sum;
double lefts[MAXN],rights[MAXN];
void qsort(int l,int r)
{
int ll=l,rr=r;
double mid=lefts[(l+r)/2];
while (ll<=rr)
{
while (lefts[ll]<mid) ll++;
while (lefts[rr]>mid) rr--;
if (ll<=rr)
{
swap(lefts[ll],lefts[rr]);
swap(rights[ll],rights[rr]);
ll++;
rr--;
}
}
if (ll<r) qsort(ll,r);
if (rr>l) qsort(l,rr);
}
void solve()
{
double std;
qsort(1,n);
sum=1;
std=rights[1];
for (int i=2;i<=n;i++)
{
if (lefts[i]>std)
{
std=rights[i];
sum++;
}
else
{
if (rights[i]<std)
{
std=rights[i];
}
}
}
}
int main()
{
int t=1;
while(1)
{
cin>>n>>d;
if (n==0&&d==0) break;
int fail=0;
for (int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
if (y>d)
{
fail=1;
}
else
{
double l=sqrt((double)(d*d-y*y));
lefts[i]=x-l;
rights[i]=x+l;
}
}
if (fail)
{
cout<<"Case "<<t++<<": "<<-1<<endl;
}
else
{
solve();
cout<<"Case "<<t++<<": "<<sum<<endl;
}
}
return 0;
}
位运算+bfs
本来用iterator来做队列的指针,但是莫名其妙的错误..
于是用了int来做指针..安心啊.
#include <iostream>
#include <vector>
using namespace std;
bool hash[65536];
vector<int> answer;
int fstate[16]={
0xF888,0xF444,0xF222,0xF111,
0x8F88,0x4F44,0x2F22,0x1F11,
0x88F8,0x44F4,0x22F2,0x11F1,
0x888F,0x444F,0x222F,0x111F};
struct state
{
int value;
int change;
int father;
};
vector<state> list;
void jude(int v)
{
if (v==0)
{
int iter=list.size()-1;
while(iter!=0)
{
answer.push_back(list.at(iter).change);
iter=list.at(iter).father;
}
cout<<answer.size()<<endl;
for (int i=answer.size()-1;i>=0;--i)
{
cout<<answer.at(i)/4+1<<" "<<answer.at(i)%4+1<<endl;
}
int cc;cin>>cc;
exit(0);
}
}
void init()
{
int x=0,u=1;
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
{
char ch;
cin>>ch;
int k;
switch(ch)
{
case '-':k=0;break;
case '+':k=1;break;
default:break;
}
x=x*2+k;
}
hash[x]=true;
state st;
st.value=x;
st.change=-1;
st.father=0;
list.push_back(st);
jude(x);
}
void bfs()
{
vector<state>::iterator iter=list.begin();
int left=0;
while (left<list.size())
{
state st=list.at(left);
int value=st.value;
for (int i=0;i<16;i++)
{
int n=fstate[i]^value;
if (hash[n]) continue;
state sta;
sta.value=n;
sta.father=left;
sta.change=i;
hash[n]=true;
list.push_back(sta);
jude(n);
}
left++;
}
}
int main()
{
init();
bfs();
return 0;
}