uva 825 Walking on the Safe Side
2014-06-02 20:53
369 查看
简单的动态规划题目,就是数据输入格式有点麻烦
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string.h>
using namespace std;
vector< vector<char> > arr;
int case_count;
void func(int m, int n){
int *dp;
int i, j, sum;
static int case_n = 1;
dp = (int*)malloc(sizeof(int)*(n+1));
if(arr[1][1])
dp[1] = 0;
else
dp[1] = 1;
for(i=1; i<=m; i++){
for(j=1; j<=n; j++){
if(1==i && 1==j)
continue;
sum = 0;
if(j-1>=1 && !arr[i][j-1])
sum += dp[j-1];
if(i-1>=1 && !arr[i-1][j])
sum += dp[j];
dp[j] = sum;
}
}
if(case_n != 1)
printf("\n");
printf("%d\n", dp
);
free(dp);
case_n++;
}
int main(void){
int case_n;
int m, n, i, j;
int ii, jj;
int row, col, len, num;
vector<char> v;
vector<int> vv;
char buf[200];
//freopen("input.dat", "r", stdin);
scanf("%d", &case_n);
case_count = case_n;
while(case_n--){
scanf("%d %d", &m, &n);
getchar();
arr.clear();
v.clear();
for(j=0; j<=n; j++) v.push_back(0);
for(i=0; i<=m; i++) arr.push_back(v);
for(i=1; i<=m; i++){
gets(buf);
len = strlen(buf);
ii = 0;
vv.clear();
while(1){
while(1){
if(buf[ii]>='0' && buf[ii]<='9')
break;
ii++;
}
if(ii >= len)
break;
sscanf(buf+ii, "%d", &num);
vv.push_back(num);
while(1){
if( !(buf[ii]>='0' && buf[ii]<='9') )
break;
ii++;
}
if(ii >= len)
break;
}
for(ii=1; ii<vv.size(); ii++){
arr[vv[0]][vv[ii]] = 1;
}
}
func(m, n);
}
return 0;
}
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string.h>
using namespace std;
vector< vector<char> > arr;
int case_count;
void func(int m, int n){
int *dp;
int i, j, sum;
static int case_n = 1;
dp = (int*)malloc(sizeof(int)*(n+1));
if(arr[1][1])
dp[1] = 0;
else
dp[1] = 1;
for(i=1; i<=m; i++){
for(j=1; j<=n; j++){
if(1==i && 1==j)
continue;
sum = 0;
if(j-1>=1 && !arr[i][j-1])
sum += dp[j-1];
if(i-1>=1 && !arr[i-1][j])
sum += dp[j];
dp[j] = sum;
}
}
if(case_n != 1)
printf("\n");
printf("%d\n", dp
);
free(dp);
case_n++;
}
int main(void){
int case_n;
int m, n, i, j;
int ii, jj;
int row, col, len, num;
vector<char> v;
vector<int> vv;
char buf[200];
//freopen("input.dat", "r", stdin);
scanf("%d", &case_n);
case_count = case_n;
while(case_n--){
scanf("%d %d", &m, &n);
getchar();
arr.clear();
v.clear();
for(j=0; j<=n; j++) v.push_back(0);
for(i=0; i<=m; i++) arr.push_back(v);
for(i=1; i<=m; i++){
gets(buf);
len = strlen(buf);
ii = 0;
vv.clear();
while(1){
while(1){
if(buf[ii]>='0' && buf[ii]<='9')
break;
ii++;
}
if(ii >= len)
break;
sscanf(buf+ii, "%d", &num);
vv.push_back(num);
while(1){
if( !(buf[ii]>='0' && buf[ii]<='9') )
break;
ii++;
}
if(ii >= len)
break;
}
for(ii=1; ii<vv.size(); ii++){
arr[vv[0]][vv[ii]] = 1;
}
}
func(m, n);
}
return 0;
}
相关文章推荐
- UVA 825 Walking on the Safe Side
- UVa 825 Walking on the Safe Side (DP)
- UVA 825 - Walking on the Safe Side(重做)
- UVa 825 Walking on the Safe Side(DP)
- UVA - 825 Walking on the Safe Side
- UVA 825 Walking on the Safe Side 读入输出+DP
- dp uva-825-Walking on the Safe Side
- uva 825 Walking on the Safe Side(dp)
- uva 825 - Walking on the Safe Side(dp)
- Walking on the Safe Side - UVa 825 dp
- UVa 825 - Walking on the Safe Side
- uva 825 - Walking on the Safe Side(DP,输入好坑。。。)
- UVa 825 - Walking on the Safe Side
- uva 825 Walking on the Safe Side(动态规划:记忆化搜索)
- UVA 825 Walking on the Safe Side
- uva 825 - Walking on the Safe Side
- UVA 825 Walking on the Safe Side(记忆化搜索)
- UVA 825 Walking on the Safe Side(记忆化搜索)
- UVA825- Walking on the Safe Side
- uva_825_Walking on the Safe Side