Snowman
ACM常用函数freopen--文件读写
snowman 发表于 2010-03-15 18:07:40
函数名:freopen
声明:FILE *freopen( const char *path, const char *mode, FILE *stream );
所在文件: stdio.h
参数说明:
path: 文件名。
mode: 文件打开的模式。和fopen中的模式(如r, w,)相同。
stream: 一个文件,通常使用标准流文件(stdin, stdout, stderr)。
返回值:成功,则返回一个path所指定的文件的指针。失败,返回NULL。(一般都不使用它的返回值)
功能:实现重定向。把预定义的标准流文件(stdin, stdout, stderr)定向到由path指定的文件中。
例
#include <stdio.h>
int main(void)
{
char text[33];
freopen("input.txt", "r", stdin); //读数据
freopen("output.txt", "w", stdout); //注释掉此句则输出到控制台
while(scanf("%s", text) != EOF){
printf("%s", text);
}
return 0;
}
收藏:
QQ书签
del.icio.us
业余技能学习
snowman 发表于 2009-12-24 17:33:59
1、利用ps、coreldraw、Flash进行图像处理和设计
2、ppt模板设计与制作
3、毕业论文整理
4、编写实用小程序
收藏:
QQ书签
del.icio.us
zoj 1002 Fire Net
snowman 发表于 2008-12-27 16:16:35
原题:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2
算法:回溯
#include <stdio.h>
int board[4][4], n, ans, count;
/*0 空地 1 墙 2碉堡
programed by snowman 2008.12.27
*/
int islegal(int x, int y)
{
int i;
if(board[x][y] == 1 || board[x][y] == 2) return 0;
i = x; i --;
while( i >= 0)
{
if(board[i][y] == 2) return 0;
else if(board[i][y] == 1) break;
i --;
}
i = x; i ++;
while( i < n)
{
if(board[i][y] == 2) return 0;
else if(board[i][y] == 1) break;
i ++;
}
i = y; i --;
while( i >= 0)
{
if(board[x][i] == 2) return 0;
else if(board[x][i] == 1) break;
i --;
}
i = y; i ++;
while( i < n)
{
if(board[x][i] == 2) return 0;
else if(board[x][i] == 1) break;
i ++;
}
return 1;
}
void dfs()
{
int i, j;
for( i = 0; i < n; i ++)
for(j = 0; j < n; j ++)
if(islegal(i, j))
{
board[i][j] = 2;
count ++;
dfs();
count --;
board[i][j] = 0;
}
if(count > ans) ans = count ;
}
int main()
{
char str[5];
int i, j;
while(scanf("%d", &n), n)
{
for(i = 0; i < n; i ++)
{
scanf("%s", str);
for(j = 0; j < n; j ++)
if(str[j] == '.') board[i][j] = 0;
else board[i][j] = 1;
}
ans = 0; count = 0;
dfs();
printf("%d\n", ans);
}
return 1;
}
算法:回溯
#include <stdio.h>
int board[4][4], n, ans, count;
/*0 空地 1 墙 2碉堡
programed by snowman 2008.12.27
*/
int islegal(int x, int y)
{
int i;
if(board[x][y] == 1 || board[x][y] == 2) return 0;
i = x; i --;
while( i >= 0)
{
if(board[i][y] == 2) return 0;
else if(board[i][y] == 1) break;
i --;
}
i = x; i ++;
while( i < n)
{
if(board[i][y] == 2) return 0;
else if(board[i][y] == 1) break;
i ++;
}
i = y; i --;
while( i >= 0)
{
if(board[x][i] == 2) return 0;
else if(board[x][i] == 1) break;
i --;
}
i = y; i ++;
while( i < n)
{
if(board[x][i] == 2) return 0;
else if(board[x][i] == 1) break;
i ++;
}
return 1;
}
void dfs()
{
int i, j;
for( i = 0; i < n; i ++)
for(j = 0; j < n; j ++)
if(islegal(i, j))
{
board[i][j] = 2;
count ++;
dfs();
count --;
board[i][j] = 0;
}
if(count > ans) ans = count ;
}
int main()
{
char str[5];
int i, j;
while(scanf("%d", &n), n)
{
for(i = 0; i < n; i ++)
{
scanf("%s", str);
for(j = 0; j < n; j ++)
if(str[j] == '.') board[i][j] = 0;
else board[i][j] = 1;
}
ans = 0; count = 0;
dfs();
printf("%d\n", ans);
}
return 1;
}
收藏:
QQ书签
del.icio.us
